DAY16 代码随想录算法训练营第十六天 104.二叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数

文章介绍了如何计算二叉树的最大深度和最小深度,分别使用了递归和层序遍历的方法。对于完全二叉树的节点个数,提供了两种解法,一种是普通递归,时间复杂度为O(N),另一种利用完全二叉树的特性,时间复杂度优化到O(logN)。
摘要由CSDN通过智能技术生成

104.二叉树的最大深度 Maximun depth of Binary Tree

深度和高度求法都差不多,递归法的话用左右中遍历就可以了.

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

111.二叉树的最小深度 Minimun Depth of Binary Tree

层序遍历

回顾一下层序遍历怎么写

class Solution {  
public:  
    int minDepth(TreeNode* root) {  
        if (root == NULL){  
            return 0;  
        }  
        queue<TreeNode*> que;  
        que.push(root);  
        int size = 0;  
        int depth = 0;  
        while (!que.empty()){  
            size = que.size();  
            depth++;  
            while (size--){  
                TreeNode* node = que.front();  
                que.pop();  
                if (node->left) {  
                    que.push(node->left);  
                }  
                if (node->right){  
                    que.push(node->right);  
                }  
                if (!node->left && !node->right){  
                    return depth;  
                }  
            }  
        }  
        return depth;  
    }  
};
后序遍历
class Solution {
public:
    int minDepth(TreeNode* root) {
        if (root == NULL){
            return 0;
        }
        int leftdepth = minDepth(root->left);
        int rightdepth = minDepth(root->right);
        if (!root->left && root->right){
            return 1 + rightdepth;
        }
        if (root->left && !root->right){
            return 1 + leftdepth;
        }       
        return 1 + min(leftdepth, rightdepth);
    }
};

222.完全二叉树的节点个数 Count Complete Tree Nodes

写完很有感悟

普通递归法 时间复杂度O(N):
class Solution {  
public:  
    int count(TreeNode* node){  
        if (node == NULL){  
            return 0;  
        }  
        int leftcount = count(node->left);  
        int rightcount = count(node->right);  
        int value = leftcount + rightcount + 1;  
        return value;  
    }  
    int countNodes(TreeNode* root) {  
        if (root == NULL){  
            return 0;  
        }  
        return count(root);  
    }  
};
根据完全二叉树特性 时间复杂度O(log N):
class Solution {  
public:  
    int count(TreeNode* node){  
        if (node == NULL) return 0;  
        TreeNode* left = node->left;  
        TreeNode* right = node->right;  
        int leftdepth = 0, rightdepth = 0;  
        while (left){  
            left = left->left;  
            leftdepth++;  
        }  
        while(right){  
            right=right->right;  
            rightdepth++;  
        }  
        if (leftdepth == rightdepth){  
            return (2 << leftdepth) - 1;  
        }  
        return count(node->left) + count(node->right) + 1;  
    }  
    int countNodes(TreeNode* root) {  
        if (root == NULL){  
            return 0;  
        }  
        return count(root);  
    }  
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Stark_Ye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值