22## 题目地址
33https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
44
5- ## 思路
5+ > 和求最大深度一个套路?
66
7- 看完了这篇 [ 二叉树:看看这些树的最大深度 ] ( ) ,再来看看如何求最小深度。
7+ # 111.二叉树的最小深度
88
9- 直觉上好像和求最大深度差不多,其实还是差挺多的,一起来看一下。
9+ 给定一个二叉树,找出其最小深度。
10+
11+ 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
12+
13+ 说明: 叶子节点是指没有子节点的节点。
14+
15+ 示例:
16+
17+ 给定二叉树 [ 3,9,20,null,null,15,7] ,
18+
19+ <img src =' ../pics/111.二叉树的最小深度1.png ' width =600 > </img ></div >
20+
21+ 返回它的最小深度 2.
22+
23+ # 思路
24+
25+ 看完了这篇[ 二叉树:看看这些树的最大深度] ( https://mp.weixin.qq.com/s/guKwV-gSNbA1CcbvkMtHBg ) ,再来看看如何求最小深度。
26+
27+ 直觉上好像和求最大深度差不多,其实还是差不少的。
28+
29+ 遍历顺序上依然是后序遍历(因为要比较递归返回之后的结果),但在处理中间节点的逻辑上,最大深度很容易理解,最小深度可有一个误区,如图:
30+
31+ <img src =' ../pics/111.二叉树的最小深度.png ' width =600 > </img ></div >
32+
33+ 这就重新审题了,题目中说的是:** 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。** ,注意是** 叶子节点** 。
34+
35+ 什么是叶子节点,左右孩子都为空的节点才是叶子节点!
36+
37+ ## 递归法
1038
1139来来来,一起递归三部曲:
1240
@@ -32,59 +60,63 @@ if (node == NULL) return 0;
3260
33613 . 确定单层递归的逻辑
3462
35- 这块和求最大深度可就不一样了,一些同学可能会写如下代码
36-
63+ 这块和求最大深度可就不一样了,一些同学可能会写如下代码:
3764```
3865int leftDepth = getDepth(node->left);
3966int rightDepth = getDepth(node->right);
40- if (node->left == NULL && node->right != NULL) {
41- return 1 + rightDepth;
42- }
43- if (node->left != NULL && node->right == NULL) {
44- return 1 + leftDepth;
45- }
46- return 1 + min(leftDepth, rightDepth);
67+ int result = 1 + min(leftDepth, rightDepth);
68+ return result;
4769```
4870
49- 首先取左右子树的深度,如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。
71+ 这个代码就犯了此图中的误区:
72+
73+ <img src =' ../pics/111.二叉树的最小深度.png ' width =600 > </img ></div >
5074
51- 反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。 最后如果左右子树都不为空,返回左右子树深度最小值 + 1 就可以了 。
75+ 如果这么求的话,没有左孩子的分支会算为最短深度 。
5276
53- 可以看出:求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑。
77+ 所以,如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。
78+
79+ 反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。 最后如果左右子树都不为空,返回左右子树深度最小值 + 1 。
5480
5581代码如下:
5682
5783```
58- int leftDepth = getDepth(node->left);
59- int rightDepth = getDepth(node->right);
84+ int leftDepth = getDepth(node->left); // 左
85+ int rightDepth = getDepth(node->right); // 右
86+ // 中
87+ // 当一个左子树为空,右不为空,这时并不是最低点
6088if (node->left == NULL && node->right != NULL) {
6189 return 1 + rightDepth;
6290}
91+ // 当一个右子树为空,左不为空,这时并不是最低点
6392if (node->left != NULL && node->right == NULL) {
6493 return 1 + leftDepth;
6594}
66- return 1 + min(leftDepth, rightDepth);
95+ int result = 1 + min(leftDepth, rightDepth);
96+ return result;
6797```
6898
69- 整体递归代码如下:
70-
71- ## C++代码
99+ 遍历的顺序为后序(左右中),可以看出:** 求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑。**
72100
73- ### 递归
101+ 整体递归代码如下:
74102```
75103class Solution {
76104public:
77105 int getDepth(TreeNode* node) {
78106 if (node == NULL) return 0;
107+ int leftDepth = getDepth(node->left); // 左
108+ int rightDepth = getDepth(node->right); // 右
109+ // 中
79110 // 当一个左子树为空,右不为空,这时并不是最低点
80- if (node->left == NULL && node->right != NULL) {
81- return 1 + getDepth(node->right) ;
82- }
111+ if (node->left == NULL && node->right != NULL) {
112+ return 1 + rightDepth ;
113+ }
83114 // 当一个右子树为空,左不为空,这时并不是最低点
84- if (node->left != NULL && node->right == NULL) {
85- return 1 + getDepth(node->left) ;
115+ if (node->left != NULL && node->right == NULL) {
116+ return 1 + leftDepth ;
86117 }
87- return 1 + min(getDepth(node->left), getDepth(node->right));
118+ int result = 1 + min(leftDepth, rightDepth);
119+ return result;
88120 }
89121
90122 int minDepth(TreeNode* root) {
@@ -93,15 +125,36 @@ public:
93125};
94126```
95127
96- ### 迭代法
128+ 精简之后代码如下:
97129
98- 可以使用二叉树的广度优先遍历(层序遍历),来做这道题目,其实这就是一道模板题了,二叉树的层序遍历模板在这道题解中[ 0102.二叉树的层序遍历] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0102.二叉树的层序遍历.md )
130+ ```
131+ class Solution {
132+ public:
133+ int minDepth(TreeNode* root) {
134+ if (root == NULL) return 0;
135+ if (root->left == NULL && root->right != NULL) {
136+ return 1 + minDepth(root->right);
137+ }
138+ if (root->left != NULL && root->right == NULL) {
139+ return 1 + minDepth(root->left);
140+ }
141+ return 1 + min(minDepth(root->left), minDepth(root->right));
142+ }
143+ };
144+ ```
145+
146+ ** 精简之后的代码根本看不出是哪种遍历方式,所以依然还要强调一波:如果对二叉树的操作还不熟练,尽量不要直接照着精简代码来学。**
147+
148+ ## 迭代法
99149
100- 层序遍历如图所示:
150+ 相对于 [ 二叉树:看看这些树的最大深度 ] ( https://mp.weixin.qq.com/s/guKwV-gSNbA1CcbvkMtHBg ) ,本题还可以使用层序遍历的方式来解决,思路是一样的。
101151
102- ![ 最小深度] ( https://img-blog.csdnimg.cn/20200811194719677.png )
152+ 如果对层序遍历还不清楚的话,可以看这篇:[ 二叉树:层序遍历登场!] ( https://mp.weixin.qq.com/s/Gb3BjakIKGNpup2jYtTzog )
153+
154+ ** 需要注意的是,只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点**
103155
104156代码如下:(详细注释)
157+
105158```
106159class Solution {
107160public:
@@ -112,8 +165,8 @@ public:
112165 queue<TreeNode*> que;
113166 que.push(root);
114167 while(!que.empty()) {
115- int size = que.size(); // 必须要这么写,要固定size大小
116- depth++;
168+ int size = que.size();
169+ depth++; // 记录最小深度
117170 int flag = 0;
118171 for (int i = 0; i < size; i++) {
119172 TreeNode* node = que.front();
0 commit comments