11## 题目地址
22https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
33
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+ 本题其实也要后序遍历(左右中),依然是因为要通过递归函数的返回值做计算树的高度。
530
6- ### 递归法
731按照递归三部曲,来看看如何来写。
832
9331 . 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型。
@@ -12,69 +36,71 @@ https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
1236```
1337int getDepth(TreeNode* node)
1438```
39+
15402 . 确定终止条件:如果为空节点的话,就返回0,表示高度为0。
1641
1742代码如下:
1843```
1944if (node == NULL) return 0;
2045```
2146
22- 3 . 确定单层递归的逻辑:先求它的左子树的深度,再求的右子树的深度,最后去左右深度最大的数值 +1 就是目前节点为根节点的树的深度。
47+ 3 . 确定单层递归的逻辑:先求它的左子树的深度,再求的右子树的深度,最后取左右深度最大的数值 再 +1 (加1是因为算上当前中间节点) 就是目前节点为根节点的树的深度。
2348
2449代码如下:
2550
2651```
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); // 中
3055return depth;
3156```
3257
33- 所以整体代码如下 :
58+ 所以整体C++代码如下 :
3459
3560```
3661class Solution {
3762public:
3863 int getDepth(TreeNode* node) {
3964 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;
4169 }
4270 int maxDepth(TreeNode* root) {
4371 return getDepth(root);
4472 }
4573};
4674```
4775
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+ };
4985
50- 在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示:
86+ ```
5187
52- ![ 层序遍历 ] ( https://img-blog.csdnimg.cn/20200810193056585.png )
88+ ** 精简之后的代码根本看不出是哪种遍历方式,也看不出递归三部曲的步骤,所以如果对二叉树的操作还不熟练,尽量不要直接照着精简代码来学。 **
5389
54- 所以这道题依然是一道模板题,依然可以使用二叉树层序遍历的模板来解决的。
5590
56- 我总结的算法模板会放到这里 [ leetcode刷题攻略 ] ( https://github.com/youngyangyang04/leetcode-master ) ,大家可以去看一下。
91+ ## 迭代法
5792
58- 代码如下:
93+ 使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。
5994
60- ## C++代码
95+ 在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示:
6196
62- ### 递归
97+ ![ 层序遍历 ] ( https://img-blog.csdnimg.cn/20200810193056585.png )
6398
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 )
76102
77- ### 迭代法
103+ C++代码如下:
78104
79105```
80106class Solution {
@@ -85,8 +111,8 @@ public:
85111 queue<TreeNode*> que;
86112 que.push(root);
87113 while(!que.empty()) {
88- int size = que.size(); // 必须要这么写,要固定size大小
89- depth++; // 记录深度
114+ int size = que.size();
115+ depth++; // 记录深度
90116 for (int i = 0; i < size; i++) {
91117 TreeNode* node = que.front();
92118 que.pop();
@@ -99,4 +125,66 @@ public:
99125};
100126```
101127
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+ ```
102190> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
0 commit comments