day16 104.二叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数

文章介绍了如何使用递归方法计算二叉树的最大深度(从根到叶子节点的最长路径)和最小深度(最近叶子节点的最短路径),以及完全二叉树的节点个数。通过层序和后序遍历,定义了Solution类中的相应函数实现这些计算。
摘要由CSDN通过智能技术生成

104.二叉树的最大深度

深度:从根节点到叶子节点(**使用前序(中左右)**从上到下,取决于深度从0还是1开始)
高度:从叶子节点到根节点(**使用后序(左右中)**从下到上,取决于高度从0还是1开始)
根节点的高度=二叉树的最大深度
使用层序遍历,求深度

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root==NULL) return 0;    //数组为空的情况
        queue<TreeNode*> que;
        int depth=0;
        if(root!=NULL) que.push(root);
        while(!que.empty()){
            int size=que.size();
            depth++;
            while(size--){
                TreeNode* node=que.front();
                que.pop();
                if(node->left) que.push(node->left);
                if(node->right) que.push(node->right);
            }
        }
        return depth;
    }
};

使用后序(左右中)来计算树的高度
中间的处理过程返回给父节点,父节点就知道孩子的高度,再+1,就是二叉树的高度
递归三部曲:
确定递归函数的参数和返回值,参数传入的是树的根节点,返回值是int类型
确定终止条件:如果为空节点,就返回0,表示高度是0
确定单层递归的逻辑:先求它的左子树深度,再求右子树深度,最后取左右子树最大的数组再+1(+1是要计算上当前父节点)

lass Solution {
public:
    int getdepth(TreeNode* node){
        if(node==NULL) return 0;
        int leftdepth=getdepth(node->left);
        int rightdepth=getdepth(node->right);
        int depth=1+max(leftdepth,rightdepth);
        return depth;
    }
    int maxDepth(TreeNode* root) {
        return getdepth(root);      
    }
};

111.二叉树的最小深度

最小深度:从根节点到最近叶子节点的最短路径上的节点数量
叶子节点:左右孩子都为空的节点
在这里插入图片描述
1.使用层序遍历

class Solution {
public:
    int minDepth(TreeNode* root) {
        if(root==NULL) return 0;    //数组为空的情况
        queue<TreeNode*> que;
        int depth=0;
        if(root!=NULL) que.push(root);
        while(!que.empty()){
            int size=que.size();
            depth++;
            while(size--){
                TreeNode* node=que.front();
                que.pop();
                if(node->left) que.push(node->left);
                if(node->right) que.push(node->right);
                if(!node->left && !node->right){    //当左右孩子都为空时 说明是最低点
                    return depth;
                }
            }
        }
        return depth;
    }
};

2.使用后序遍历

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);
    }
};

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

使用后序遍历,在递归左右之后,遍历中时做出操作

class Solution {
public:
    int getnum(TreeNode* node){
        if(node==NULL)return 0;
        int leftnum=getnum(node->left); //左
        int rightnum=getnum(node->right);   //右
        int treenum=leftnum+rightnum+1; //遍历中时处理数量
        return treenum;
    }
    int countNodes(TreeNode* root) {
        return getnum(root);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值