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

  • 今日学习的文章链接,或者视频链接

第六章 二叉树part03

  • 自己看到题目的第一想法

  • 看完代码随想录之后的想法

104:

前序遍历解法:

class Solution {
public:
    int depth = 0;
    int res = 0;
    // 遍历二叉树
    void traverse(TreeNode* root) {
        if (root == nullptr) {
            return;
        }

        // 前序遍历位置
        depth++;
        // 遍历的过程中记录最大深度
        res = max(res, depth);
        traverse(root->left);
        traverse(root->right);
        // 后序遍历位置
        depth--;
    }
    int maxDepth(TreeNode* root) {
        traverse(root);
        return res;
    }
};

559 后续遍历解法:

class Solution {
public:
    int maxDepth(Node* root) {
        if (root == nullptr) {
            return 0;
        }
        int subTreeMaxDepth = 0;
        for (Node* child : root->children) {
            subTreeMaxDepth = max(subTreeMaxDepth, maxDepth(child));
        }
        return 1 + subTreeMaxDepth;
    }
};

111:最小深度(后序+层序迭代)

class Solution {
public:
    int getDepth(TreeNode* node) {
        if (node == NULL) return 0;
        int leftDepth = getDepth(node->left);           // 左
        int rightDepth = getDepth(node->right);         // 右
                                                        // 中
        // 当一个左子树为空,右不为空,这时并不是最低点
        if (node->left == NULL && node->right != NULL) { 
            return 1 + rightDepth;
        }   
        // 当一个右子树为空,左不为空,这时并不是最低点
        if (node->left != NULL && node->right == NULL) { 
            return 1 + leftDepth;
        }
        int result = 1 + min(leftDepth, rightDepth);
        return result;
    }
    int minDepth(TreeNode* root) {
        return getDepth(root);
        /*
        queue<TreeNode*> que;
        if (root == NULL) return 0;
        if (root != NULL) que.push(root);
        int result=1;
        while(!que.empty()){
            int size = que.size();
            vector<int> cur_level;
            while(size--){
                TreeNode* node = que.front();
                que.pop();
                cur_level.push_back(node->val);
                if(node->left) que.push(node->left);
                if(node->right) que.push(node->right);
                if(node->left==NULL && node->right == NULL){
                    return result;
                }
            }
            result++;
        }
        return result;
        */
    }
};

222:

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr){
            return 0;
        }
        TreeNode* l = root->left;
        TreeNode* r = root->right;
        // 记录左、右子树的高度
        int hl = 0, hr = 0;
        while (l != nullptr) {
            l = l->left;
            hl++;
        }
        while (r != nullptr) {
            r = r->right;
            hr++;
        }
        // 如果左右子树的高度相同,则是一棵满二叉树
        if (hl == hr) {
            return (2 << hl) - 1;
        }
        // 如果左右高度不同,则按照普通二叉树的逻辑计算
        return 1 + countNodes(root->left) + countNodes(root->right);
    }
};
  • 自己实现过程中遇到哪些困难

  • 今日收获,记录一下自己的学习时长

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值