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

目录:

解题及思路学习

104.二叉树的最大深度

https://leetcode.cn/problems/maximum-depth-of-binary-tree/

给定一个二叉树 root ,返回其最大深度。

二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。

示例 1:

!https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg

输入:root = [3,9,20,null,null,15,7]
输出:3

思考:用层序遍历比较简单。

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

递归的方法:

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

        int left = maxDepth(root->left);
        int right = maxDepth(root->right);

        return max(left, right) + 1;
    }
};

思路基本差不多。

111.二叉树的最小深度

https://leetcode.cn/problems/minimum-depth-of-binary-tree/

给定一个二叉树,找出其最小深度。

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

**说明:**叶子节点是指没有子节点的节点。

示例 1:

!https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg

输入:root = [3,9,20,null,null,15,7]
输出:2

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

什么是叶子节点,左右孩子都为空的节点才是叶子节点!

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

遍历的顺序为后序(左右中),可以看出:求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑。

迭代法

class Solution {
public:
    int minDepth(TreeNode* root) {
        queue<TreeNode*> que;
        int depth = 0;
        if (root != NULL) que.push(root);
        while(!que.empty()) {
            int size = que.size();
            depth++;
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
                if (node->left == NULL && node->right == NULL) {
                    return depth;
                }
            }
        }
        return depth;
    }
};

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

https://leetcode.cn/problems/count-complete-tree-nodes/

给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。

完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。

示例 1:

!https://assets.leetcode.com/uploads/2021/01/14/complete.jpg

输入:root = [1,2,3,4,5,6]
输出:6

思考:用遍历迭代方法,将每层的个数加起来,这种想法比较直观。

class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        int result = 0;
        if (root != NULL) que.push(root);
        while(!que.empty()) {
            int size = que.size();
            result += size;
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return result;
    }
};
  • 时间复杂度:O(n)
  • 空间复杂度:O(n)

递归的写法:

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

        int left = countNodes(root->left);
        int right = countNodes(root->right);

        return left + right + 1;
    }
};
  • 时间复杂度:O(n)
  • 空间复杂度:O(log n),算上了递归系统栈占用的空间

复盘总结

个人反思

递归和层序遍历以及掌握的还可以了。主要是具体问题需要具体分析,稍微修改下代码。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值