代码随想录算法训练营第十五天 |LC104. 二叉树的最大深度 LC559. N 叉树的最大深度 LC111. 二叉树的最小深度 LC222. 完全二叉树的节点个数

104. 二叉树的最大深度

思路

  1. 递归:为空就停止,否则就递归,左孩子和右孩子深度最大值,还要包括根节点
  2. 迭代:层序遍历,看有多少层在第一个while循环中加

代码

  1. 递归
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == nullptr) return 0;
        return max(maxDepth(root->left), maxDepth(root->right)) + 1;
    }
};
  1. 迭代
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (!root) return 0;
        queue<TreeNode*> q;
        q.push(root);
        int depth = 0;
        while (!q.empty()){
            int size = q.size();
            depth ++;
            
            for (int i = 0; i < size; i ++){
                auto a = q.front();
                q.pop();
                if (a->left) q.push(a->left);
                if (a->right) q.push(a->right);
            }

        }
        return depth;
    }
};

559. N 叉树的最大深度

思路

  1. 递归:同上,只不过这次所有孩子都要比较
  2. 迭代:同上,只不过每次都要放入多个孩子都队列里

代码

  1. 递归
class Solution {
public:
    int maxDepth(Node* root) {
        if (root == nullptr) return 0;
        int res = 0;
        for (auto a:root->children){
            res = max(res, maxDepth(a));
        }
        return res + 1;
    }
};
  1. 迭代
class Solution {
public:
    int maxDepth(Node* root) {
        if (!root) return 0;
        queue<Node*> q;
        q.push(root);
        int depth = 0;
        while (!q.empty()){
            int size = q.size();
            depth ++;

            for (int i = 0; i < size; i ++){
                auto a = q.front();
                q.pop();
                for (int j = 0; j < a->children.size(); j ++)
                    if (a->children[j]) q.push(a->children[j]);
            }
        }
        return depth;
    }
};

111. 二叉树的最小深度

思路

  1. 递归:主要是注意节点左或者右孩子为空的时候,最小深度不是为0,因此要特判一下如果是左空右非空,左非空右空,这个时候要返回非空的值
  2. 递归:层序遍历,遍历到这个节点,它的左右孩子都是空节点,那么就是最小值

代码

  1. 递归
class Solution {
public:
    int minDepth(TreeNode* root) {
        if (root == nullptr) return 0;
        int left = INT_MAX, right = INT_MAX;
        if (root->left && root->right == nullptr) return minDepth(root->left) + 1;
        if (root->right && root->left == nullptr) return minDepth(root->right) + 1;
        return min(minDepth(root->left), minDepth(root->right))+ 1;
    }
};
  1. 迭代
class Solution {
public:
    int minDepth(TreeNode* root) {
        if (!root) return 0;
        queue<TreeNode* > q;
        q.push(root);
        int depth = 0;

        while (!q.empty()){
            int size = q.size();
            depth ++;

            for (int i = 0; i < size; i ++){
                auto a = q.front();
                q.pop();
                if (a->left) q.push(a->left);
                if (a->right) q.push(a->right);
                if (!a->left && !a->right) return depth;
            }
        }
        return depth;
    }
};

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

思路

  1. 递归:为空为截止条件,算左孩子和右孩子右多少个几点,还要包括自己的
  2. 迭代:层序遍历,要在for循环中
  3. 完全二叉树:满二叉树2^h - 1,所以每次判断一下左孩子,右孩子深度是否相同,相同就是满二叉树,可以加上去,否则就递归左右孩子,总能遇到满的情况,最差只有一层。

代码

  1. 递归
class Solution {
public:
    int countNodes(TreeNode* root) {
        if (!root) return 0;
        int left = countNodes(root->left);
        int right = countNodes(root->right);
        return left + right + 1;
    }
};
  1. 迭代
class Solution {
public:
    int countNodes(TreeNode* root) {
        if (!root) return 0;
        queue<TreeNode*> q;
        q.push(root);
        int res = 0;
        while(!q.empty()){
            int size = q.size();
            for (int i = 0; i < size; i ++){
                auto a = q.front();
                q.pop();
                res ++;
                if (a->left) q.push(a->left);
                if (a->right) q.push(a->right);
            }
        }
        return res;
    }
};
  1. 完全二叉树性质
class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr) return 0;
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        int a = 0, b = 0;
        while(left){
            left = left->left;
            a ++;
        }
        while (right){
            right = right->right;
            b ++;
        }
        if (a == b){
            return (2 << a) - 1;
        }
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值