day14 | 100.二叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数

一、二叉树的最大深度

100.二叉树的最大深度
在这里插入图片描述
因为题给出的高度和深度是一样的,所以求得结果都能过。

class Solution
{
public:
    int getHeight(TreeNode *node)
    {
        if (node == nullptr)
            return 0;
        int leftHeight = getHeight(node->left);       // 左
        int rightHeight = getHeight(node->right);     // 右
        int hight = max(leftHeight, rightHeight) + 1; // 中
        return hight;
    }
    int maxDepth(TreeNode *root)
    {
        return getHeight(root);
    }
};

二、二叉树的最小深度

111.二叉树的最小深度
注意:最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
最小高度也是复合我们求取的最小深度。

也是采用后序遍历,容易犯的错误:
在这里插入图片描述

错误的写法:
class Solution
{
public:
    int getHeight(TreeNode *node)
    {
        if (node == nullptr)
            return 0;
        int leftHeight = getHeight(node->left);
        int rightHeight = getHeight(node->right);
        int height = min(leftHeight, rightHeight) + 1;
        return height;
    }
    int minDepth(TreeNode *root)
    {
        return getHeight(root);
    }
};

确定递归函数的参数和返回值:

int getHeight(Treenode* node){}

确定终止条件:

int getHeight(Treenode* node){

}
class Solution
{
public:
    int getHeight(TreeNode *node)
    {
        if (node == nullptr)
            return 0;
        int leftHeight = getHeight(node->left);  // 左
        int rightHeight = getHeight(node->right);// 右
                                                      // 根
        if (node->left == nullptr && node->right != nullptr)
        {
            return 1 + rightHeight; // +1 是为了算上当前节点
        }
        if (node->left != nullptr && node->right == nullptr)
        {
            return 1 + leftHeight; // +1 是为了算上当前节点
        }

        return 1 + min(leftHeight, rightHeight); // +1 是为了算上当前节点
    }

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

三、完全二叉树的节点数

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

1️⃣普通二叉树的写法:
利用后序遍历来写

class Solution
{
public:
    int getNum(TreeNode *node)
    {
        if (node == nullptr)
            return 0;
        int leftnum = getNum(node->left);
        int rightnum = getNum(node->right);
        int num = leftnum + rightnum + 1;
        return num;
    }
    int countNodes(TreeNode *root)
    {
        return getNum(root);
    }
};

2️⃣利用完全二叉树的特性:
代码随想录 完全二叉树节点数的计算

满二叉树节点的计算公式:n = 2h -1
如果整个树不是满二叉树,就递归其左右孩子,直到遇到满二叉树为止,用公式计算这个子树(满二叉树)的节点数量。

如何去判断一个左子树或者右子树是不是满二叉树呢?
在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树。

递归函数的参数和返回值:

int countNodes(TreeNode* node){}

两种终止条件:
第一种:

遇到节点为空的情况
if (root == nullptr) return 0;

第二种:

遇到满二叉树的情况
TreeNode *left = node->left;
TreeNode *right = node->right;
int leftDepth = 0;
int rightDepth = 0;

while (left)
{
    left = left->left;
    leftDepth++;
}
while (right)
{
    right = right->right;
    rightDepth++;
}
if (rightDepth == leftDepth)
{
    return (2 << leftDepth) - 1;
}

单层递归逻辑:

return countNodes(node->left) + countNodes(node->right) + 1;

完整代码:

class Solution
{
public:
    int countNodes(TreeNode *node)
    {
        if (node == nullptr)
            return 0;
        TreeNode *left = node->left;
        TreeNode *right = node->right;
        int leftDepth = 0;
        int rightDepth = 0;

        while (left)
        {
            left = left->left;
            leftDepth++;
        }
        while (right)
        {
            right = right->right;
            rightDepth++;
        }
        if (rightDepth == leftDepth)
        {
            return (2 << leftDepth) - 1;
        }
        int leftnum=countNodes(node->left); // 左
        int rightnum=countNodes(node->right); // 右
        int num = leftnum + rightnum + 1; // 根
        return num;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值