-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxDepth.go
executable file
·44 lines (41 loc) · 815 Bytes
/
maxDepth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package binaryTree
//func maxDepth(root *TreeNode) int {
// sum := 0
// var maxfunc func(rootNode *TreeNode) int
// maxfunc = func(rootNode *TreeNode) int {
// if rootNode.Left == nil && rootNode.Right == nil {
// return 1
// }
// return max(maxfunc(rootNode.Left), maxfunc(rootNode.Right))
//
// }
// return sum + maxfunc(root)
//}
//func max(x, y int) int {
// if x >= y {
// return x
// }
// return y
//}
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
if root.Left == nil && root.Right == nil {
return 1
}
var depthLeft, depthRight int
if root.Left != nil {
depthLeft = maxDepth(root.Left)
depthLeft++
}
if root.Right != nil {
depthRight = maxDepth(root.Right)
depthRight++
}
depth := depthLeft
if depthLeft < depthRight {
depth = depthRight
}
return depth
}