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

104.二叉树的最大深度

104. 二叉树的最大深度

1.层序遍历

每层遍历完++

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        int depth=0;
        if(root==NULL) return depth;

        TreeNode* cur = root;
        queue<TreeNode*> que;
        que.push(cur);
        while(!que.empty()){
            int size = que.size();
            for(int i=0; i<size; i++){
                cur = que.front();
                que.pop();
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
            depth++;
        }
        
        return depth;
    }
};

2.递归

单层递归的逻辑:先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int getdepth(TreeNode* root){
        if(root==NULL) return 0;
        int leftdepth = getdepth(root->left);
        int rightdepth = getdepth(root->right);
        return 1+max(leftdepth, rightdepth);
    }
    int maxDepth(TreeNode* root) {
        return getdepth(root);
    }
};

111.二叉树的最小深度

111. 二叉树的最小深度

1.递归

最小深度是从根节点到最近叶子节点的最短路径上的节点数量

左右孩子都为空的节点才是叶子节点,如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。 最后如果左右子树都不为空,返回左右子树深度最小值 + 1 。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int getdepth(TreeNode* root){
        if(root==NULL) return 0;

        int leftdepth = getdepth(root->left);
        int rightdepth = getdepth(root->right);
        if(root->left==NULL && root->right!=NULL){
            return 1+rightdepth;
        }
        if(root->left!=NULL && root->right==NULL){
            return 1+leftdepth;
        }
        return 1+min(rightdepth, leftdepth);

    }
    int minDepth(TreeNode* root) {
        return getdepth(root);
    }
};

2.迭代-层序遍历

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {
        if(!root) return 0;
        queue<TreeNode*> que;
        que.push(root);
        int count = 0;
        
        while(!que.empty()){
            int size = que.size();
            count++;
            for(int i=0;i<size;i++){
                TreeNode* cur = que.front();
                que.pop();
                if(!cur->left&&!cur->right)return count;
                if(cur->left)que.push(cur->left);
                if(cur->right)que.push(cur->right);

            }
            
        }
        return count;
    }
};

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

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

在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树。

递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root==NULL) return 0;

        TreeNode* left = root->left;
        TreeNode* right = root->right;

        int leftsum = 0;
        int rightsum = 0;

        while(left){
            left = left->left;
            leftsum++;
        }
        while(right){
            right = right->right;
            rightsum++;
        }

        if(leftsum==rightsum) return (2<<leftsum)-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、付费专栏及课程。

余额充值