1
1
## 题目地址
2
2
https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
3
3
4
- ## 思路
4
+ > “简单题”系列
5
+
6
+ 看完本篇可以一起做了如下两道题目:
7
+ * 104.二叉树的最大深度
8
+ * 559.N叉树的最大深度
9
+
10
+ # 104.二叉树的最大深度
11
+
12
+ 给定一个二叉树,找出其最大深度。
13
+
14
+ 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
15
+
16
+ 说明: 叶子节点是指没有子节点的节点。
17
+
18
+ 示例:
19
+ 给定二叉树 [ 3,9,20,null,null,15,7] ,
20
+
21
+ <img src =' ../pics/104. 二叉树的最大深度.png ' width =600 > </img ></div >
22
+
23
+ 返回它的最大深度 3 。
24
+
25
+ # 思路
26
+
27
+ ## 递归法
28
+
29
+ 本题其实也要后序遍历(左右中),依然是因为要通过递归函数的返回值做计算树的高度。
5
30
6
- ### 递归法
7
31
按照递归三部曲,来看看如何来写。
8
32
9
33
1 . 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型。
@@ -12,69 +36,71 @@ https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
12
36
```
13
37
int getDepth(TreeNode* node)
14
38
```
39
+
15
40
2 . 确定终止条件:如果为空节点的话,就返回0,表示高度为0。
16
41
17
42
代码如下:
18
43
```
19
44
if (node == NULL) return 0;
20
45
```
21
46
22
- 3 . 确定单层递归的逻辑:先求它的左子树的深度,再求的右子树的深度,最后去左右深度最大的数值 +1 就是目前节点为根节点的树的深度。
47
+ 3 . 确定单层递归的逻辑:先求它的左子树的深度,再求的右子树的深度,最后取左右深度最大的数值 再 +1 (加1是因为算上当前中间节点) 就是目前节点为根节点的树的深度。
23
48
24
49
代码如下:
25
50
26
51
```
27
- int leftDepth = getDepth(node->left);
28
- int rightDepth = getDepth(node->right);
29
- int depth = 1 + max(leftDepth, rightDepth);
52
+ int leftDepth = getDepth(node->left); // 左
53
+ int rightDepth = getDepth(node->right); // 右
54
+ int depth = 1 + max(leftDepth, rightDepth); // 中
30
55
return depth;
31
56
```
32
57
33
- 所以整体代码如下 :
58
+ 所以整体C++代码如下 :
34
59
35
60
```
36
61
class Solution {
37
62
public:
38
63
int getDepth(TreeNode* node) {
39
64
if (node == NULL) return 0;
40
- return 1 + max(getDepth(node->left), getDepth(node->right));
65
+ int leftDepth = getDepth(node->left); // 左
66
+ int rightDepth = getDepth(node->right); // 右
67
+ int depth = 1 + max(leftDepth, rightDepth); // 中
68
+ return depth;
41
69
}
42
70
int maxDepth(TreeNode* root) {
43
71
return getDepth(root);
44
72
}
45
73
};
46
74
```
47
75
48
- ### 迭代法
76
+ 代码精简之后C++代码如下:
77
+ ```
78
+ class Solution {
79
+ public:
80
+ int maxDepth(TreeNode* root) {
81
+ if (root == NULL) return 0;
82
+ return 1 + max(maxDepth(root->left), maxDepth(root->right));
83
+ }
84
+ };
49
85
50
- 在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示:
86
+ ```
51
87
52
- ![ 层序遍历 ] ( https://img-blog.csdnimg.cn/20200810193056585.png )
88
+ ** 精简之后的代码根本看不出是哪种遍历方式,也看不出递归三部曲的步骤,所以如果对二叉树的操作还不熟练,尽量不要直接照着精简代码来学。 **
53
89
54
- 所以这道题依然是一道模板题,依然可以使用二叉树层序遍历的模板来解决的。
55
90
56
- 我总结的算法模板会放到这里 [ leetcode刷题攻略 ] ( https://github.com/youngyangyang04/leetcode-master ) ,大家可以去看一下。
91
+ ## 迭代法
57
92
58
- 代码如下:
93
+ 使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。
59
94
60
- ## C++代码
95
+ 在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示:
61
96
62
- ### 递归
97
+ ![ 层序遍历 ] ( https://img-blog.csdnimg.cn/20200810193056585.png )
63
98
64
- ```
65
- class Solution {
66
- public:
67
- int getDepth(TreeNode* node) {
68
- if (node == NULL) return 0;
69
- return 1 + max(getDepth(node->left), getDepth(node->right));
70
- }
71
- int maxDepth(TreeNode* root) {
72
- return getDepth(root);
73
- }
74
- };
75
- ```
99
+ 所以这道题的迭代法就是一道模板题,可以使用二叉树层序遍历的模板来解决的。
100
+
101
+ 如果对层序遍历还不清楚的话,可以看这篇:[ 二叉树:层序遍历登场!] ( https://mp.weixin.qq.com/s/Gb3BjakIKGNpup2jYtTzog )
76
102
77
- ### 迭代法
103
+ C++代码如下:
78
104
79
105
```
80
106
class Solution {
@@ -85,8 +111,8 @@ public:
85
111
queue<TreeNode*> que;
86
112
que.push(root);
87
113
while(!que.empty()) {
88
- int size = que.size(); // 必须要这么写,要固定size大小
89
- depth++; // 记录深度
114
+ int size = que.size();
115
+ depth++; // 记录深度
90
116
for (int i = 0; i < size; i++) {
91
117
TreeNode* node = que.front();
92
118
que.pop();
@@ -99,4 +125,66 @@ public:
99
125
};
100
126
```
101
127
128
+ 那么我们可以顺便解决一下N叉树的最大深度问题
129
+
130
+ # 559.N叉树的最大深度
131
+ https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/
132
+
133
+ 给定一个 N 叉树,找到其最大深度。
134
+
135
+ 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
136
+
137
+ 例如,给定一个 3叉树 :
138
+
139
+ <img src =' ../pics/559.N叉树的最大深度.png ' width =600 > </img ></div >
140
+
141
+ 我们应返回其最大深度,3。
142
+
143
+ # 思路
144
+
145
+ 依然可以提供递归法和迭代法,来解决这个问题,思路是和二叉树思路一样的,直接给出代码如下:
146
+
147
+ ## 递归法
148
+
149
+ C++代码:
150
+
151
+ ```
152
+ class Solution {
153
+ public:
154
+ int maxDepth(Node* root) {
155
+ if (root == 0) return 0;
156
+ int depth = 0;
157
+ for (int i = 0; i < root->children.size(); i++) {
158
+ depth = max (depth, maxDepth(root->children[i]));
159
+ }
160
+ return depth + 1;
161
+ }
162
+ };
163
+ ```
164
+ ## 迭代法
165
+
166
+ 依然是层序遍历,代码如下:
167
+
168
+ ```
169
+ class Solution {
170
+ public:
171
+ int maxDepth(Node* root) {
172
+ queue<Node*> que;
173
+ if (root != NULL) que.push(root);
174
+ int depth = 0;
175
+ while (!que.empty()) {
176
+ int size = que.size();
177
+ depth++; // 记录深度
178
+ for (int i = 0; i < size; i++) {
179
+ Node* node = que.front();
180
+ que.pop();
181
+ for (int i = 0; i < node->children.size(); i++) {
182
+ if (node->children[i]) que.push(node->children[i]);
183
+ }
184
+ }
185
+ }
186
+ return depth;
187
+ }
188
+ };
189
+ ```
102
190
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
0 commit comments