【未完】day16-● 104.二叉树的最大深度 559.n叉树的最大深度● 111.二叉树的最小深度● 222.完全二叉树的节点个数

104.二叉树的最大深度  

104. 二叉树的最大深度 - 力扣(LeetCode)

区分深度和高度:

深度:结点到根节点的距离(前序遍历求)

高度:结点到叶子节点的距离(后序遍历求)

根节点的高度就是二叉树的最大深度

方法一:广度优先搜索(层序遍历)

就是在层序遍历的过程中增加一个计数器,一层遍历完后加一,最后得到了一共有多少层,就是二叉树的最大深度

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == nullptr)return 0;
        //层序遍历 统计一共遍历了几层
        queue<TreeNode*> que;
        que.push(root);
        int size;
        int count = 0;
        while (!que.empty()) {
            size = que.size();
            count++;
            while (size--) {
                TreeNode* cur;
                cur = que.front();
                que.pop();
                if (cur->left)que.push(cur->left);
                if (cur->right)que.push(cur->right);
            }
        }
        return count;
    }
};

方法二:递归后序遍历

后序遍历实际上求的是高度,根节点的高度就是二叉树的最大深度

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == nullptr)return 0;
        int maxleft, maxright;
        maxleft = maxDepth(root->left);
        maxright = maxDepth(root->right);
        return 1 + max(maxleft, maxright);
    }
};

559.n叉树的最大深度

559. N 叉树的最大深度 - 力扣(LeetCode)

【此题后续做】

111.二叉树的最小深度

111. 二叉树的最小深度 - 力扣(LeetCode)

关注最小深度的定义:根节点到最近的叶子节点的高度

class Solution {
public:
    int minDepth(TreeNode* root) {
        if (root == nullptr)return 0;
        int leftlenth = minDepth(root->left);
        int rightlenth = minDepth(root->right);
        if (root->left == nullptr && root->right != nullptr) {
            return 1 + rightlenth;
        }
        if (root->left != nullptr && root->right == nullptr) {
            return 1 + leftlenth;
        }
        int result = 1 + min(rightlenth, leftlenth);
        return result;
    }
};

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

方法一

  1. 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回以该节点为根节点二叉树的节点数量,所以返回值为int类型。
  2. 确定终止条件:如果为空节点的话,就返回0,表示节点数为0。
  3. 确定单层递归的逻辑:先求它的左子树的节点数量,再求右子树的节点数量,最后取总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量。
class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr)return 0;
        //后序遍历
        int leftnum = countNodes(root->left);
        int rightnum = countNodes(root->right);
        return leftnum + rightnum + 1;
    }
};

方法二

我们知道满二叉树求节点个数有一个公式,是2^层数-1,在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树,相比于第一个方法,本方法只遍历了二叉树两侧的结点,没有遍历到中间的结点

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr)return 0;
        int deepleft = 0, deepright = 0;
        //不可省略这一步,不然会改变根节点指向
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        while (left != nullptr) {
            left = left->left;
            deepleft++;
        }
        while (right != nullptr) {
            right = right->right;
            deepright++;
        }
        //是满二叉树
        if (deepleft == deepright)return pow(2, (int)(deepleft + 1)) - 1;
        //一次循环 后序遍历
        int leftnum = countNodes(root->left);
        int rightnum = countNodes(root->right);
        return leftnum + rightnum + 1;
    }
};

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值