-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path55-I.go
54 lines (48 loc) · 846 Bytes
/
55-I.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
45
46
47
48
49
50
51
52
53
54
package main
/***
"题目:**二叉树的深度**
[二叉树的深度](https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof)
题目描述:
***/
/**
解法一
说明:递归左子树与右子树
**/
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
return max(maxDepth(root.Left), maxDepth(root.Right)) + 1
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
/**
解法二
说明:队列
**/
func maxDepth2(root *TreeNode) int {
if root == nil {
return 0
}
var queue []*TreeNode
queue = append(queue, root)
res := 0
for len(queue) > 0 {
num := len(queue)
for i := 0; i < num; i++ {
if queue[i].Left != nil {
queue = append(queue, queue[i].Left)
}
if queue[i].Right != nil {
queue = append(queue, queue[i].Right)
}
}
queue = queue[num:]
res++
}
return res
}