Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 57a2696

Browse files
aQuaaQua
aQua
authored and
aQua
committed
104 finish
思维定式害死人
1 parent 47bedf6 commit 57a2696

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

Algorithms/0104.maximum-depth-of-binary-tree/maximum-depth-of-binary-tree.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,16 @@ import (
77
type TreeNode = kit.TreeNode
88

99
func maxDepth(root *TreeNode) int {
10-
res := 0
11-
12-
var dfs func(*TreeNode, int)
13-
dfs = func(root *TreeNode, level int) {
14-
if root == nil {
15-
return
16-
}
17-
18-
// 出现了新的 level
19-
if res < level {
20-
res = level
21-
}
22-
23-
dfs(root.Left, level+1)
24-
dfs(root.Right, level+1)
10+
if root == nil {
11+
return 0
2512
}
2613

27-
dfs(root, 1)
14+
return 1 + max(maxDepth(root.Left), maxDepth(root.Right))
15+
}
2816

29-
return res
17+
func max(a, b int) int {
18+
if a > b {
19+
return a
20+
}
21+
return b
3022
}

Algorithms/0104.maximum-depth-of-binary-tree/maximum-depth-of-binary-tree_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ func Test_Problem0104(t *testing.T) {
3030
3,
3131
},
3232

33+
{
34+
[]int{3, 9, 20, 15, 10, 7},
35+
[]int{9, 3, 10, 15, 20, 7},
36+
4,
37+
},
38+
3339
// 可以多个 testcase
3440
}
3541

0 commit comments

Comments
 (0)