代码随想录算法训练营第十六天| 二叉树的层序遍历十题 LeetCode104.二叉树的最大深度 559.n叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数

104.二叉树的最大深度

题目:104. 二叉树的最大深度

//层序遍历法
// class Solution {
// public:
//     int maxDepth(TreeNode* root) {
//         int ans = 0;
//         queue<TreeNode*> q;
//         if(root == nullptr) return 0;
//         q.push(root);
//         while(!q.empty()){
//             int size = q.size();
//             while(size--){
//                 TreeNode* cur = q.front();
//                 q.pop();
//                 if(cur->left) q.push(cur->left);
//                 if(cur->right) q.push(cur->right);
//             }
//             ans++;
//         }   
//         return ans;
//     }
// };

//递归法,后序遍历
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root == nullptr) return 0;
        int left_h = maxDepth(root->left);
        int right_h = maxDepth(root->right);
        return 1+ max(left_h,right_h);
    }
};

559.n叉树的最大深度

题目:559. N 叉树的最大深度

class Solution {
public:
    int maxDepth(Node* root) {
        if(root == nullptr) return 0;
        vector<Node*> chil = root->children;
        int d_max = 0;
        for(Node* c : chil){
            if(c != nullptr){
                d_max = max(d_max,maxDepth(c));
            }
        }
        return d_max + 1;
    }
};

111.二叉树的最小深度

题目:111. 二叉树的最小深度

//层序遍历法
// class Solution {
// public:
//     int minDepth(TreeNode* root) {
//         int ans = 0;
//         queue<TreeNode*> q;
//         if(root == nullptr) return 0;
//         q.push(root);
//         while(!q.empty()){
//             int size = q.size();
//             while(size--){
//                 TreeNode* cur = q.front();
//                 q.pop();
//                 if(cur->left) q.push(cur->left);
//                 if(cur->right) q.push(cur->right);
//                 if(cur->left == nullptr && cur->right == nullptr)return ans+1;
//             }
//             ans++;
//         }   
//         return ans;
//     }
// };

//递归法,后序遍历
class Solution {
public:
    int minDepth(TreeNode* root) {
        if(root == nullptr) return 0;
        int left_h = minDepth(root->left);
        int right_h = minDepth(root->right);
        if(root->left == nullptr && root->right != nullptr) return 1 + right_h;
        if(root->right == nullptr && root->left != nullptr) return 1 + left_h;
        return 1 + min(left_h,right_h);
    }
};

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

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

//递归法
class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root == nullptr) return 0;
        int left_num = countNodes(root->left);
        int right_num = countNodes(root->right);
        return left_num + right_num + 1;
    }
}

//层序遍历
class Solution {
public:
    int countNodes(TreeNode* root) {
        int ans = 0;
        queue<TreeNode*> q;
        if(root == nullptr) return 0;
        q.push(root);
        while(!q.empty()){
            int size = q.size();
            while(size--){
                ans++;
                TreeNode* cur = q.front();
                q.pop();
                if(cur->left) q.push(cur->left);
                if(cur->right) q.push(cur->right);
            }
        }
        return ans;
    }
};

//用完全二叉树特性做优化
class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr) return 0;
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
        while (left) {  // 求左子树深度
            left = left->left;
            leftDepth++;
        }
        while (right) { // 求右子树深度
            right = right->right;
            rightDepth++;
        }
        if (leftDepth == rightDepth) {
            return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
        }
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};

总结

题型:二叉树的最大深度,最小深度,节点个数

技巧:后序遍历求解,注意最小深度的特殊情况。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值