Skip to content

Commit 7d2ffbc

Browse files
committed
maximum_depth_of_binary_tree
1 parent cee8700 commit 7d2ffbc

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
9090
#### [101. symmetric tree](https://github.com/hitzzc/go-leetcode/tree/master/symmetric_tree)
9191
#### [102. Binary Tree Level Order Traversal](https://github.com/hitzzc/go-leetcode/tree/master/binary_tree_level_order_traversal)
9292
#### [103. Binary Tree Zigzag Level Order Traversal](https://github.com/hitzzc/go-leetcode/tree/master/binary_tree_zigzag_level_order_traversal)
93+
#### [104. maximum depth of binary tree](https://github.com/hitzzc/go-leetcode/tree/master/maximum_depth_of_binary_tree)
9394

9495

9596

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package maximum_depth_of_binary_tree
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func maxDepth(root *TreeNode) int {
10+
if root == nil {
11+
return 0
12+
}
13+
if root.Left == nil && root.Right == nil {
14+
return 1
15+
}
16+
left := maxDepth(root.Left)
17+
right := maxDepth(root.Right)
18+
if left > right {
19+
return left + 1
20+
}
21+
return right + 1
22+
}

0 commit comments

Comments
 (0)