算法训练day16|二叉树part03(LeetCode104.二叉树的最大深度(后序递归的应用)、LeetCode111.二叉树的最小深度、LeetCode222.完全二叉树的节点个数)

104.二叉树的最大深度

题目链接🔥
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

1.后序递归通过高度求深度

1.思路分析

深度是任意节点到根节点的距离,根节点的深度是1。
高度是任意节点到叶子结点的距离,叶子结点的高度是1。

要求高度应该采用后序遍历,左右中,把子节点的高度返回给父节点,则父节点的高度就是子节点加一。
要求深度应该采用前序遍历,中左右,把父节点的高度返回给子节点,则子节点的深度就是父节点加一。

📎解题思路就是根节点的高度就是二叉树的最大深度。

递归三步走

  1. 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型。
  2. 确定终止条件:如果为空节点的话,就返回0,表示高度为0。
  3. 确定单层递归的逻辑:先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。

2.代码实现

class Solution {
public:
    int maxDepth(TreeNode* root) {
    if(root==nullptr) return 0;
    int leftdepth=maxDepth(root->left);
    int rightdepth=maxDepth(root->right);
    int depth=1+max(leftdepth,rightdepth);
    return depth;
    }
};

还有一种更精简的写法:

class Solution {
public:
    int maxDepth(TreeNode* root) {
    if(root==nullptr) return 0;
    return 1+max(maxDepth(root->left),maxDepth(root->right));
    }
};

但是这种不太好理解,也不太好体现出来后序遍历的思路。

相关的有一个n叉树的最大深度

递归法代码:

class Solution {
public:
    int maxDepth(Node* root) {
        if(root==nullptr) return 0;
        int depth=0;
        for(int i=0;i<root->children.size();i++){
            //depth=1+max(depth,maxDepth(root->children[i]));
            //是不对的,一层的子节点加一次一就可以了
            depth=max(depth,maxDepth(root->children[i]));
        }
        return depth+1;     
    }
};

层序遍历法可以看层序遍历,有LeetCode上使用层序遍历解的十道题。

2.迭代法

使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,记录一下遍历的层数就是二叉树的深度。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        queue<TreeNode*> que;
        int depth=0;
        if(root) que.push(root);
        while(!que.empty()){
            int size=que.size();
            while(size--){
                TreeNode* cur=que.front();
                que.pop();            
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);  
            }    
            depth++; 
        }
        return depth;        
    }
};

这里可以看层序遍历,有LeetCode上使用层序遍历解的十道题。

3.复杂度分析

  • 时间复杂度

所有节点遍历一遍,复杂度O(n)

  • 空间复杂度

空间复杂度主要取决于递归时栈空间的开销,最坏情况下,树呈现链状,空间复杂度为 O(N)。平均情况下树的高度与节点数的对数正相关,空间复杂度为O(logN)。

4.总结思考

💡求高度是从下往上数的,收集左右孩子信息,向上返回的这类思路都需要用到后序遍历,所以求高度用后序遍历(左右中)
💡本题的递归法也可以用前序遍历来写,但是会复杂很多,有回溯,

class solution {
public:
    int result;
    void getdepth(treenode* node, int depth) {
        result = depth > result ? depth : result; // 中
 
        if (node->left == NULL && node->right == NULL) return ;
 
        if (node->left) { // 左
            depth++;    // 深度+1
            getdepth(node->left, depth);
            depth--;    // 回溯,深度-1
        }
        if (node->right) { // 右
            depth++;    // 深度+1
            getdepth(node->right, depth);
            depth--;    // 回溯,深度-1
        }
        return ;
    }
    int maxdepth(treenode* root) {
        result = 0;
        if (root == NULL) return result;
        getdepth(root, 1);
        return result;
    }
};
 
# 简化后如下
class solution {
public:
    int result;
    void getdepth(treenode* node, int depth) {
        result = depth > result ? depth : result; // 中
        if (node->left == NULL && node->right == NULL) return ;
        if (node->left) { // 左
            getdepth(node->left, depth + 1);
        }
        if (node->right) { // 右
            getdepth(node->right, depth + 1);
        }
        return ;
    }
    int maxdepth(treenode* root) {
        result = 0;
        if (root == 0) return result;
        getdepth(root, 1);
        return result;
    }
};

本题学习时间⌛️:80min


111.二叉树的最小深度

题目链接🔥
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。

1.后序递归

1.思路分析

使用后序遍历,其实求的是根节点到叶子节点的最小距离,就是求高度的过程,不过这个最小距离 也同样是最小深度。
本题还有一个误区,最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
所以:
在这里插入图片描述
所以下面代码是错的:

//错误示范
class Solution {
public:
    int minDepth(TreeNode* root) {
    if(root==nullptr) return 0;
    int leftdepth=minDepth(root->left);
    int rightdepth=minDepth(root->right);
    int depth=1+min(leftdepth,rightdepth);
    return depth;
    }
};

应该加一个判断条件:

  • 如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。
  • 右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。
  • 最后如果左右子树都不为空,返回左右子树深度最小值 + 1 。

2.代码实现

class Solution {
public:
    int minDepth(TreeNode* root) {
        if(root==nullptr) return 0;
        int depth=0;
        int leftdepth=minDepth(root->left);
        int rightdepth=minDepth(root->right);
        if(root->left==nullptr&&root->right!=nullptr){
            return 1+rightdepth;
        } //左
        if(root->left!=nullptr&&root->right==nullptr){
            return 1+leftdepth;
        } //右
        depth=1+min(leftdepth,rightdepth);//中
        return depth;
    }
};
  • 时间复杂度

所有节点遍历一遍,复杂度O(n)

  • 空间复杂度

空间复杂度主要取决于递归时栈空间的开销,最坏情况下,树呈现链状,空间复杂度为 O(N)。平均情况下树的高度与节点数的对数正相关,空间复杂度为O(logN)。

2.迭代法

class Solution {
public:
    int minDepth(TreeNode* root) {
        queue<TreeNode*> que;
        int depth=0;
        if(root) que.push(root);
        while(!que.empty()){
            int size=que.size();
            depth++; 
            while(size--){
                TreeNode* cur=que.front();
                que.pop();            
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);  
                if(!cur->left&&!cur->right)  return depth;
            }           
        }
        return depth;        
    }
};

这里可以看层序遍历,有LeetCode上使用层序遍历解的十道题。

3.思考总结

💡本题的递归法也可以用前序遍历来写,但是会复杂很多,有回溯

class Solution {
private:
    int result;
    void getdepth(TreeNode* node, int depth) {
        // 函数递归终止条件
        if (root == nullptr) {
            return;
        }
        // 中,处理逻辑:判断是不是叶子结点
        if (root -> left == nullptr && root->right == nullptr) {
            res = min(res, depth);
        }
        if (node->left) { // 左
            getdepth(node->left, depth + 1);
        }
        if (node->right) { // 右
            getdepth(node->right, depth + 1);
        }
        return ;
    }

public:
    int minDepth(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }
        result = INT_MAX;
        getdepth(root, 1);
        return result;
    }
};

本题学习时间⌛️:1.3h


222.完全二叉树的节点个数

题目链接
给出一个完全二叉树,求出该树的节点个数。

示例 1:
输入:root = [1,2,3,4,5,6]
输出:6

示例 2:
输入:root = []
输出:0

示例 3:
输入:root = [1]
输出:1

1.普通二叉树

1.后序递归

确定递归函数的参数和返回值,终止条件这里就不说了,
单层递归的逻辑:先求它的左子树的节点数量,再求右子树的节点数量,最后取总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量

class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root==nullptr) return 0;
        int leftcount=countNodes(root->left);
        int rightcount=countNodes(root->right);
        int count=leftcount+rightcount+1;
        return count;
    }
};
  • 时间复杂度

所有节点遍历一遍,复杂度O(n)

  • 空间复杂度

空间复杂度主要取决于递归时栈空间的开销,最坏情况下,树呈现链状,空间复杂度为 O(N)。平均情况下树的高度与节点数的对数正相关,空间复杂度为O(logN)。

2.迭代法

加一个变量count,统计节点数量就可以了

class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        if(root) que.push(root);
        int count=0;
        while(!que.empty()){
            int size=que.size();
            while(size--){
                TreeNode* cur=que.front();
                que.pop();
                count++;
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
        }
        return count;
    }
};

时间复杂度:O(n)
空间复杂度:O(n)

2.完全二叉树法

刚才的两种解法都没有用到完全二叉树的特性,
在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。

如何利用二叉树的特性

完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。

  1. 对于情况一,可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1。

  2. 对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。

在这里插入图片描述
在这里插入图片描述

如何去判断一个左子树或者右子树是不是满二叉树呢?

在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树。如图:
在这里插入图片描述
左右深度不相等就不是满二叉树
在这里插入图片描述
这种根本就不是完全二叉树
在这里插入图片描述
整个递归的思路是:

(1)求当前树的左深度和右深度

(2)如果相等,是满二叉树,用公式返回节点数量

(3)如果不相等,就再求其左子树和右子树的节点数量,当前树的节点数量=左子树节点数量+右子树节点数量+1。由于递归到某一个深度,一定会遇到满二叉树,所以,递归到底层,一定是由公式法返回的。

class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root==nullptr) return 0;
        int leftdepth=0,rightdepth=0;// 这里初始为0是有目的的,为了下面求指数方便
        TreeNode* left=root->left;
        TreeNode* right=root->right;
        while(left){
            left=left->left;
            leftdepth++;
        }
        while(right){
            right=right->right;
            rightdepth++;
        }
        if(leftdepth==rightdepth) {
            // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
            return (2<<leftdepth)-1;
        }
        return countNodes(root->left)+countNodes(root->right)+1;
    }
};
  • 时间复杂度:O(log n × log n)

计算左子树和右子树深度的 while 循环的时间复杂度是 O(log n),其中 n 是节点的数量。在完全二叉树中,左子树或右子树的深度最多为 log n。

递归调用的时间复杂度也是 O(log n)。在完全二叉树中,每一层的节点数都是固定的,因此递归的深度也是 log n。

每次计算满二叉树的时候,计算的其实就是当前树高,平均情况下即 O(logn),因为这里是完全二叉树,不考虑最坏情况下的链式的树高为O(N)的树,每次递归调用的都是下一层的子树,总共调用了“树的高度”次,即 O(logn),所以时间复杂度为 O(logn) * O(logn);

  • 空间复杂度:O(logN)

此外,使用了递归,额外调用了栈空间,最多压栈”当前树高“个元素,所以空间复杂度为 O(logn)。

3.思考总结

💡位运算<<符号用来将一个数的各二进制位全部左移n位,左移一位就是乘2。
💡利用完全二叉树的性质来计算是没想到的,其中一个核心就是完全二叉树的左右节点的终止一定满足满二叉树

本题学习时间:90miin


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值