104.二叉树的最大深度
一、做题感受&第一想法
递归。
是一种后序遍历的模式,用左右子树的高度的最大值,加一,作为当前节点的高度。
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL) return 0;
int l = maxDepth(root->left);
int r = maxDepth(root->right);
return (l > r) ? l + 1 : r + 1;
}
};
二、学习文章后收获
1.二叉树的高度和深度
-
二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
-
二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)
-
所以,根节点的高度就是二叉树的最大深度。
2.层序遍历/迭代法
天哪!层序遍历太好用了,我竟然没想到,瘫。
class Solution {
public:
int maxDepth(TreeNode* root) {
queue<TreeNode*> q;
TreeNode* cur = NULL;
int hight = 0;
if(root) q.push(root);
while(!q.empty()){
hight++;
int size = q.size();
for(int i = 0;i < size;i++){
cur = q.front();
q.pop();
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
}
}
return hight;
}
};
3.回溯法(暂未学习)
三、过程中遇到的问题
1.C++提供max()函数
2.一种超出时间限制的情况
return语句中,重复调用了多次maxDepth函数,时间成本大大提高!!最后超出时间限制。(太蠢的代码了亲)
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL) return 0;
return maxDepth(root->left) > maxDepth(root->right) ? maxDepth(root->left) + 1 : maxDepth(root->right) + 1;
}
};
四、相关题目
- 559.N 叉树的最大深度
- 111.二叉树的最小深度:易错。 要注意当节点root不空的时候,分析四种情况:
- ①左右孩子都空,返回1;
- ②左孩子空,右孩子不空,返回右孩子高度+1;
- ③右孩子空,左孩子不空,返回左孩子高度+1;
- ④左右孩子都不空,返回二者高度最小值+1。
111.二叉树的最小深度
一、做题感受&第一想法
1.经典的错误,标准的零分
如果单纯只判断左右子树最小的值,那么如果左/右孩子中其中一个为NULL,则该结点不是叶子节点,但是返回给上层的值会是NULL对应的路径的值。
class Solution {
public:
int minDepth(TreeNode* root) {
if(root == NULL) return 0 ;
int r = minDepth(root->right);
int l = minDepth(root->left);
return min(l,r) + 1;
}
};
二、学习文章后收获
1.树的最小深度
树的最小深度:所有“叶节点“高度的最小值!!!
2.思路
对每个节点root,判断其孩子的情况:
- 如果左右孩子都为空,则其为叶子节点,返回1(可以和情况三合并)
- 如果左/右孩子中有一个为空,则不是叶子节点,返回非空节点最小高度+1
- 如果左/右孩子都不为空,则返回二者最小高度的较小值+1
3.迭代法(后序遍历的思想)
class Solution {
public:
int minDepth(TreeNode* root) {
if(root == NULL) return 0 ;
if(root->left == NULL && root->right == NULL) return 1; //情况1(其实这一行可以不写,因为可以和情况3合并)
int r = minDepth(root->right);
if(root->left == NULL && root->right != NULL) return r+1; //情况2
int l = minDepth(root->left);
if(root->left != NULL && root->right == NULL) return l+1; //情况2
return min(l,r) + 1; //情况3
}
};
4.迭代法(找所有的孩子节点,取它们深度的最小值。)
几个关注点:
- 函数参数要有本层深度depth,如果该节点为叶子节点,则会用到。
- 用了全局变量result来记录最小高度,初始为INT_MAX。
- 只有判断该节点为叶子节点时,才会修改result为当前result和depth的较小值。depth其实就是当前叶子节点的深度。所以相当于把所有的叶子节点深度比较了一遍,最后选出了最小值。
class Solution {
public:
int result;
void findMin(TreeNode* root, int depth){
if(root == NULL) return;
if(root->left == NULL && root->right == NULL){ //节点是叶子
result = min(result,depth);
return;
}
findMin(root->left,depth + 1);
findMin(root->right,depth + 1);
return;
}
int minDepth(TreeNode* root) {
if(root == NULL) return 0;
result = INT_MAX;
findMin(root, 1);
return result;
}
};
5.层序遍历(找第一个叶节点,它的高度就是树的最小深度)
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> q;
TreeNode* cur = NULL;
int depth = 0;
if(root) q.push(root);
while(!q.empty()){
int size = q.size();
depth++;
for(int i = 0;i < size;i++){
cur = q.front();
q.pop();
if(cur->left == NULL && cur->right == NULL){ //如果找到了叶节点,则返回节点的depth
return depth;
}
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
}
}
return depth;
}
};
三、过程中遇到的问题
1.cpp提供最大值接口
INT_MAX
即为int最大值。
2.cpp的类中的变量其实可当作全局变量适用
要灵活编程~
222.完全二叉树的节点个数
一、做题感受&第一想法
层序遍历。(a出来就是成功!耶
class Solution {
public:
int countNodes(TreeNode* root) {
queue<TreeNode*> q;
TreeNode* cur = NULL;
int num = 0;
if(root) q.push(root);
while(!q.empty()){
cur = q.front();
q.pop();
num++;
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
}
return num;
}
};
二、学习文章后收获
1.针对普通二叉树,求结点总数
(1)递归法
class Solution {
public:
int countNodes(TreeNode* root) {
if(root == NULL) return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
}
};
(2)层序遍历
如上。
2.针对完全二叉树,求结点总数
- 从上往下遍历完全二叉树,总会遇到满二叉树
- 对完全二叉树,如何判断它的子树是不是一个满二叉树:一路向左、一路向右,如果左右深度相同,则为满二叉树。
- 递归三部曲:
- 返回条件:找到满二叉树,则该树节点数为2^h-1(h:该子树深度)
- 本层处理:return 左+右+1
- 函数参数和返回值:返回int节点数,函数参数root。
class Solution {
public:
int countNodes(TreeNode* root) {
if(root == NULL) return 0; //防止根节点直接为空
//如果是一个满二叉树,则返回
TreeNode* l = root->left;
TreeNode* r = root->right;
int rightDepth = 0, leftDepth = 0;
while(l != nullptr){
leftDepth++;
l = l->left;
}
while(r != nullptr){
rightDepth++;
r = r->right;
}
if(rightDepth == leftDepth){
return (2 << leftDepth) - 1;
}
//如果不是一个满二叉树,则处理左右后加在一起
return 1 + countNodes(root->left) + countNodes(root->right);
}
};
三、过程中遇到的问题
1.如何计算2的n次方
方法一:
pow(2,n)
,需要头文件math.h(leetcode平台已含,不需要,可直接用)。
方法二:
2 << (n-1)
,把2左移(n-1)位;或者1 << n
,把1左移n位,以此类推。
2.运算符优先级问题导致的报错
Line 29: Char 22: runtime error: shift exponent -1 is negative (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:38:22
报错原因:return 2 << leftDepth - 1
中减号优先级高于移位符号,导致先计算了减法。
解决方案:return (2 << leftDepth) - 1
,先移位,后减法。