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

104.二叉树的最大深度

题目链接:力扣

class Solution {
public:
    int result;
    void getdepth(TreeNode*node, int depth) {
        result = result > depth ? result : depth;
        if(node == NULL) return;
        if(node->left) {
            getdepth(node->left, depth+1);
        }
        if(node->right) {
            getdepth(node->right, depth+1);
        }
    }
    int maxDepth(TreeNode* root) {
        // 使用前序遍历实现,模拟求二叉树深度的过程
        if(root == NULL) return 0;
        getdepth(root, 1);
        return result;
    }
};
class Solution {
public:
    int getheight(TreeNode* node) {
        if(node == NULL) return 0;
        int left = getheight(node->left);
        int right = getheight(node->right);
        int high = 1 + max(left, right);
        return high;
    }
    int maxDepth(TreeNode* root) {
        // 使用后序遍历来实现
        return getheight(root);
    }
};

111.二叉树的最小深度

题目链接:力扣

class Solution {
public:
    int getdepth(TreeNode* node) {
        if(node == NULL) return 0;
        int left = getdepth(node->left);
        int right = getdepth(node->right);
​
        if(node->left == NULL && node->right) return 1+right;
        else if(node->left && node->right == NULL) return 1+left;
        else return 1+min(left,right);
    }
    int minDepth(TreeNode* root) {
        // 后序遍历
        return getdepth(root);
    }
};
class Solution {
public:
    int minDepth(TreeNode* root) {
        // 迭代法(层序遍历)
        queue<TreeNode*> que;
        int depth = 0;
        if(root == NULL) return depth;
        que.push(root);
        while(!que.empty()) {
            int size = que.size();
            depth++;
            for(int i = 0; i < size; i++) {
                TreeNode* tmp = que.front();
                que.pop();
                if(tmp->left) que.push(tmp->left);
                if(tmp->right) que.push(tmp->right);
                if(!tmp->left && !tmp->right) return depth;
            }
        } 
        return depth;
    }
};

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

题目链接:力扣

class Solution {
public:
    int countNodes(TreeNode* root) {
        // 简单层序遍历统计节点
        queue<TreeNode*> que;
        if(root == NULL) return 0;
        que.push(root);
        int result = 0;
        while(!que.empty()) {
            int size = que.size();
            for(int i = 0; i < size; i++) {
                TreeNode* tmp = que.front();
                que.pop();
                result++;
                if(tmp->left) que.push(tmp->left);
                if(tmp->right) que.push(tmp->right);
            }
        }
        return result;
    }
};
class Solution {
public:
    int countNodes(TreeNode* root) {
        // 根据完全二叉树的特性来实现
        // 结束条件
        if(root == NULL) return 0;
        int leftCount = 0;
        int rightCount = 0;
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        while(left) {
            left = left->left;
            leftCount++;
        }
        while(right) {
            right = right->right;
            rightCount++;
        }
        if(rightCount == leftCount) return (2 << leftCount) - 1;
​
        // 遍历过程
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值