Skip to content

Commit 73c432d

Browse files
committed
commit solution 104
1 parent 4dedeff commit 73c432d

File tree

6 files changed

+111
-8
lines changed

6 files changed

+111
-8
lines changed

Diff for: index-tags.md

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
| --- | --- | --- | --- | --- |
142142
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
143143
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
144+
| [104](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree) | [二叉树的最大深度](/solution/100-199/0104.maximum-depth-of-binary-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
144145

145146
#### **递归**
146147

@@ -150,6 +151,7 @@
150151
| --- | --- | --- | --- | --- |
151152
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
152153
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
154+
| [104](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree) | [二叉树的最大深度](/solution/100-199/0104.maximum-depth-of-binary-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
153155

154156
#### **广度优先搜索**
155157

Diff for: index-type.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
3030
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
3131
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
32-
| [104](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree) | [二叉树的最大深度](/solution/100-199/0104.maximum-depth-of-binary-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> |
32+
| [104](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree) | [二叉树的最大深度](/solution/100-199/0104.maximum-depth-of-binary-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
3333
| [107](https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii) | [二叉树的层次遍历 ii](/solution/100-199/0107.binary-tree-level-order-traversal-ii/) | ``,`广度优先搜索` | <font color=green>简单</font> |
3434
| [108](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree) | [将有序数组转换为二叉搜索树](/solution/100-199/0108.convert-sorted-array-to-binary-search-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> |
3535
| [110](https://leetcode-cn.com/problems/balanced-binary-tree) | [平衡二叉树](/solution/100-199/0110.balanced-binary-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> |

Diff for: solution/100-199/0104.maximum-depth-of-binary-tree/README.md

+32-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
1-
# [100. xxx](https://leetcode-cn.com/problems/recover-binary-search-tree)
1+
# [104. 二叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/)
22

33
### 题目描述
44

5+
<p>给定一个二叉树,找出其最大深度。</p>
6+
7+
<p>二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。</p>
8+
9+
<p><strong>说明:</strong>&nbsp;叶子节点是指没有子节点的节点。</p>
10+
11+
<p><strong>示例:</strong><br>
12+
给定二叉树 <code>[3,9,20,null,null,15,7]</code>,</p>
13+
14+
<pre> 3
15+
/ \
16+
9 20
17+
/ \
18+
15 7</pre>
19+
20+
<p>返回它的最大深度&nbsp;3 。</p>
521

622
### 解题思路
723

24+
1. 分治
825

926
### 具体解法
1027

11-
<!-- tabs:start -->
12-
1328
#### **Golang**
1429
```go
15-
30+
type TreeNode struct {
31+
Val int
32+
Left *TreeNode
33+
Right *TreeNode
34+
}
35+
36+
func maxDepth(root *TreeNode) int {
37+
if root == nil {
38+
return 0
39+
}
40+
left := maxDepth(root.Left)
41+
right := maxDepth(root.Right)
42+
return 1 + int(math.Max(float64(left), float64(right)))
43+
}
1644
```
1745

18-
<!-- tabs:end -->
19-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package leetcode
2+
3+
import "math"
4+
5+
/*
6+
* @lc app=leetcode.cn id=104 lang=golang
7+
*
8+
* [104] 二叉树的最大深度
9+
*/
10+
11+
// @lc code=start
12+
/**
13+
* Definition for a binary tree node.
14+
* type TreeNode struct {
15+
* Val int
16+
* Left *TreeNode
17+
* Right *TreeNode
18+
* }
19+
*/
20+
type TreeNode struct {
21+
Val int
22+
Left *TreeNode
23+
Right *TreeNode
24+
}
25+
26+
func maxDepth(root *TreeNode) int {
27+
if root == nil {
28+
return 0
29+
}
30+
left := maxDepth(root.Left)
31+
right := maxDepth(root.Right)
32+
return 1 + int(math.Max(float64(left), float64(right)))
33+
}
34+
35+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMaxDepth(t *testing.T) {
8+
var ret int
9+
p := &TreeNode{
10+
Val: 2,
11+
Left: &TreeNode{
12+
Val: 3,
13+
Left: &TreeNode{
14+
Val: 4,
15+
Left: nil,
16+
Right: nil,
17+
},
18+
Right: &TreeNode{
19+
Val: 5,
20+
Left: nil,
21+
Right: nil,
22+
},
23+
},
24+
Right: &TreeNode{
25+
Val: 3,
26+
Left: nil,
27+
Right: &TreeNode{
28+
Val: 4,
29+
Left: nil,
30+
Right: nil,
31+
},
32+
},
33+
}
34+
35+
ret = 3
36+
if ret != maxDepth(p) {
37+
t.Fatalf("case fails %v\n", ret)
38+
}
39+
40+
}

Diff for: solution/100-199/_sidebar.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- [101. 对称二叉树 ✅](solution/100-199/0101.symmetric-tree/)
1111
- [102. 二叉树的层序遍历](solution/100-199/0102.binary-tree-level-order-traversal/)
1212
- [103. 二叉树的锯齿形层次遍历](solution/100-199/0103.binary-tree-zigzag-level-order-traversal/)
13-
- [104. 二叉树的最大深度](solution/100-199/0104.maximum-depth-of-binary-tree/)
13+
- [104. 二叉树的最大深度](solution/100-199/0104.maximum-depth-of-binary-tree/)
1414
- [105. 从前序与中序遍历序列构造二叉树](solution/100-199/0105.construct-binary-tree-from-preorder-and-inorder-traversal/)
1515
- [106. 从中序与后序遍历序列构造二叉树](solution/100-199/0106.construct-binary-tree-from-inorder-and-postorder-traversal/)
1616
- [107. 二叉树的层次遍历 ii](solution/100-199/0107.binary-tree-level-order-traversal-ii/)

0 commit comments

Comments
 (0)