Day16——二叉树的最大深度、二叉树的最小深度、完全二叉树的节点个数

第16天。

目录

前言

一、二叉树最大深度

解题思路2(递归):

二、二叉树的最小深度

题目链接:

三、n叉树的最大深度

四、完全二叉树的节点个数

1、解题思路(层序)

2、解题思路(递归)

总结


前言

今日文案:

人生总会有不期而遇的温暖和生生不息的希望,前提是你必须足够去努力,才不会错过美丽的风景和动人的故事,早安~


一、二叉树最大深度

题目来源:

力扣

解题思路1(层序):

我们只要广度搜索,一层一层遍历,然后一层一层计数,然后最后我们就可以找出最大深度。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        int deep=0;
        queue<TreeNode*> que;        //队列
        if(root)
        que.push(root);
        while(!que.empty())        
        {
            int size=que.size();        //每层
            for(int i=0;i<size;i++)
            {
                TreeNode*p=que.front();
                que.pop();
                if(p->left)
                que.push(p->left);
                if(p->right)
                que.push(p->right);
            }
            deep++;                    //每层加1
        }
        return deep;
    }
};

解题思路2(递归):

解题思路:

左边遍历,右边遍历,记录返回值。

class Solution {
public:
    int getdepth(TreeNode*root)
    {
        if(root==0)
        return 0;

        int leftdepth=getdepth(root->left);    //记录左边深度
        int rightdepth=getdepth(root->right);  //记录右边深度

        int res=max(leftdepth,rightdepth);        //返回最大深度
        return res+1;                        //加1 返回上一层
    } 
    int maxDepth(TreeNode* root) {
        return getdepth(root);
    }
};

二、二叉树的最小深度

题目链接:

力扣

class Solution {
public:
    int minDepth(TreeNode *root) {
        if (root == nullptr) {
            return 0;
        }

        if (root->left == nullptr && root->right == nullptr) {        //判断左右
            return 1;                                //叶子节点深度+1返回上一层
        }

        int min_depth = INT_MAX;
        if (root->left != nullptr) {        
            min_depth = min(minDepth(root->left), min_depth);
        }
        if (root->right != nullptr) {
            min_depth = min(minDepth(root->right), min_depth);
        }

        return min_depth + 1;
    }
}

三、n叉树的最大深度

题目来源:

力扣

具体思路上一篇文章有哇。

class Solution {
public:

    int maxDepth(Node* root) {
        if(root==0)
        return 0;
        int res=0;
        for(auto &x:root->children)        //把节点遍历进去
        {
            int D=maxDepth(x);
            res=max(D,res);
        }
            return res+1;
    }
};

四、完全二叉树的节点个数

题目来源:

力扣

1、解题思路(层序)

一层一层往下刷,记录数值就可以,因为已经给定了一颗完全二叉树。

class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        int result = 0;
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                result++;   // 记录节点数量
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return result;
    }
};

2、解题思路(递归)

先求它的左子树的节点数量,再求的右子树的节点数量,最后取总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量。

class Solution {
public:

    int count=0;
    int getcount(TreeNode*root)
    {
        if(root==NULL)
        {
            return 0;
        }

        int leftNode=getcount(root->left);        //清点左右节点数量
        int rightNode=getcount(root->right);
        return count=leftNode+rightNode+1;        //+1把节点数量返回上一层
    }

    int countNodes(TreeNode* root) {
        if(root==0)
        return 0;
        return getcount(root);
    }
};

总结

冲冲冲!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值