代码随想录算法训练营day16|104.二叉树的最大深度、559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数

今天这些题目用层序遍历都能很快解决,但效率一般。

首先需要明确几个知识点。

  1. 二叉树的深度(Depth):(一般采用前序遍历方式)

    • 定义:从根节点到最远叶子节点的最长路径上的节点数。
    • 注意:最远叶子节点是指最深的叶子节点,即没有子节点的节点。
    • 性质:对于任何非空二叉树,其深度至少为 1。
  2. 二叉树的高度(Height):(一般采用后序遍历方式)

    • 定义:从根节点到最远叶子节点的最长路径上的节点数。
    • 注意:与深度相同,最远叶子节点是指最深的叶子节点。
    • 性质:对于任何非空二叉树,其高度至少为 1。

二叉树的最大深度

层序遍历

        最大深度即层序遍历所能达到最大层,创建一个depth表示层,在每层遍历完后将depth加1,最后返回depth。

class Solution {
public:
    queue<TreeNode *>queue;
    int depth = 0;
    int maxDepth(TreeNode* root) {
        if(root == nullptr){
            return depth;
        } // 如果根节点为空,直接返回depth
        queue.push(root);
        while(!queue.empty()){
            int levelsize = queue.size();
            for(int i = 0; i < levelsize; i++){
                TreeNode * cur = queue.front();
                queue.pop();
                if(cur->left){
                    queue.push(cur->left);
                }
                if(cur->right){
                    queue.push(cur->right);
                }
            }
            depth++;//每一层都完成后再depth++
        }
        return depth;
    }
};

算法的时间复杂度为O(n),空间复杂度也为O(n)。

递归法

根节点的高度就是二叉树的最大深度,使用后序遍历。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root == nullptr){//传入节点为空时,返回0
            return 0;
        }
        return max(maxDepth(root->left),maxDepth(root->right))+1;//返回左右子树的最大深度+1
    }
};

二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | LeetCode:104.二叉树的最大深度_哔哩哔哩_bilibiliicon-default.png?t=N7T8https://www.bilibili.com/video/BV1Gd4y1V75u/?spm_id_from=333.788&vd_source=fc4a6e70e3a87b7ea67c2024e326e7c5

N叉树的最大深度

层序遍历

        同样,使用层序遍历,与二叉树相比的区别在于孩子节点的个数,考虑到孩子节点是用数组来表示,将数组中的元素依次加入队列,其他代码与上题无异。

class Solution {
public:
    int maxDepth(Node* root) {
        queue<Node *>queue;
        int depth = 0;
        if(root == nullptr){
            return depth;
        } // 如果根节点为空,直接返回depth
        queue.push(root);
        while(!queue.empty()){
            int levelsize = queue.size();
            for(int i = 0; i < levelsize; i++){
                Node * cur = queue.front();
                queue.pop();
                for(auto x:cur->children){
                    queue.push(x);
                }
            }
            depth++;//每一层都完成后再depth++
        }
        return depth;
    }
};

递归法

这里与二叉树的最大深度的区别就是节点的孩子数量,考虑使用一个数组保存孩子数量,同样使用递归。

class Solution {
public:
     int getdepth(Node *root) {
        if (root == nullptr) { // 如果节点为空,返回0
            return 0;
        }
        int ans = 0; // 初始化返回值ans为0
        for (auto x : root->children) { // 遍历节点的所有子节点
            ans = max(ans, getdepth(x)); // 递归调用getdepth函数,计算每个子节点的深度,并取最大值
        }
        return ans + 1; // 返回最大深度加1,即当前节点的深度
    }
    int maxDepth(Node* root) {
        int depth = getdepth(root);
        return depth;
    }
};

算法的时间复杂度为O(n),空间复杂度也为O(n)。

二叉树的最小深度

层序遍历

        这里使用层序遍历需要加一个判断条件,最小深度是找到距离根节点层最小的叶节点,叶节点的定义是无孩子节点的节点,所以在每次遍历层时,只要发现有一个节点无孩子节点,即可直接返回深度depth。

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

算法的时间复杂度为O(n),空间复杂度也为O(n)。

递归法

参考代码随想录。

看起来好像做过,一写就错! | LeetCode:111.二叉树的最小深度_哔哩哔哩_bilibiliicon-default.png?t=N7T8https://www.bilibili.com/video/BV1QD4y1B7e2/?spm_id_from=333.788&vd_source=fc4a6e70e3a87b7ea67c2024e326e7c5

class Solution {
public:
    int minDepth(TreeNode* root) {
        if(root == nullptr){
            return 0;
        }
        int leftheight = minDepth(root->left);
        int rightheight = minDepth(root->right);
        if(root->left == nullptr and root->right!=nullptr){
            return 1+rightheight;
        }
        if(root->right == nullptr and root->left!=nullptr){
            return 1+leftheight;
        }
        int result = 1 + min(leftheight,rightheight);
        return result; 
    }
};

算法的空间复杂度和时间复杂度均为O(n)。

完全二叉树的个数

层序遍历

将每层的节点相加,即能得到二叉树节点的个数。

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

算法的时间复杂度为O(n),空间复杂度也为O(n)。

递归法

创建一个count代表总的数量,每遍历到一个节点,count++,且本题前中后序都行。

class Solution {
public:
    int count;
    int countNodes(TreeNode* root) {
        if(root == nullptr){//传入节点为空时,返回0
            return 0;
        }
        count++;//当遍历到一个非空节点,count++;
        countNodes(root->left);
        countNodes(root->right);
        return count;//返回最后的count
    }
};

算法的时间复杂度和空间复杂度都为O(n)。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值