Day16|二叉树part03:104.二叉树的最大深度 559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数

104. 二叉树的最大深度

leetcode链接:力扣题目链接(opens new window)

视频链接:二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度 (opens new window)

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

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

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

示例:
给定二叉树 [3,9,20,null,null,15,7]3
   / \
  9  20
    /  \
   15   7
返回它的最大深度 3

经历了昨天的层次遍历,这道题在之前也做过,有两种方法。递归法(前/后序遍历)和迭代法(层次遍历)

这里需要注意的是:

  • 深度(后序遍历) :当前节点到根节点的距离;
  • 高度(前序遍历):当前节点到叶子节点的距离。

递归法(分解法)

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

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

参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型:

int getdepth(TreeNode* node)
  1. 确定终止条件

如果为空节点的话,就返回0,表示高度为0。

if (node == NULL) return 0;
  1. 确定单层递归逻辑

先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。

int leftdepth = getdepth(node->left);       // 左
int rightdepth = getdepth(node->right);     // 右
int depth = 1 + max(leftdepth, rightdepth); // 中
return depth;

总的代码:

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }
        // 利用定义,计算左右子树的最大深度
        int leftMax = maxDepth(root->left);
        int rightMax = maxDepth(root->right);
        // 整棵树的最大深度等于左右子树的最大深度取最大值,
        // 然后再加上根节点自己
      //这个属于后序遍历
        int res = max(leftMax, rightMax) + 1;

        return res;
    }
};

迭代法(层次遍历)

class Solution {
public:
    int maxDepth(TreeNode *root) {
        queue<TreeNode *> q;
        int depth = 0;
        if (root == nullptr) {
            return depth;
        }
        q.push(root);
        while (!q.empty()) {
            depth++;
            unsigned int sz = q.size();
            for (int i = 0; i < sz; i++) {
                TreeNode *cur = q.front();
                q.pop();
                if (cur->left != nullptr) {
                    q.push(cur->left);
                }
                if (cur->right != nullptr) {
                    q.push(cur->right);
                }
            }
        }
        return depth;
    }
};

559.N叉树的最大深度

同理,也可以用递归法和迭代法。只需要修改几行代码

递归法

修改遍历的左右子树改成for:

class Solution {
public:
    int maxDepth(Node* root) {
        if(root == nullptr){
            return 0;
        }
        int depth = 0;
        for(auto child : root->children){
            depth = max(depth, maxDepth(child)) ;
        }
        return depth + 1;
    }
}

迭代法

入队的时候左右子树改成for:

class Solution {
public:
    int maxDepth(Node* root) {
        queue<Node *> q;
        int depth = 0;
        if (root == nullptr) {
            return depth;
        }
        q.push(root);
        while (!q.empty()) {
            depth++;
            unsigned int sz = q.size();
            for (int i = 0; i < sz; i++) {
                Node *cur = q.front();
                q.pop();
                for(auto child: cur->children){
                    if(child != nullptr){
                        q.push(child);
                    }
                }
            }
        }
        return depth;

    }
};

111. 二叉树的最小深度

求最小,适合用层序遍历BFS来解,当节点的左右子树都为空时,说明是叶子节点,就最早找到最小深度了:

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

            }
        }
       return depth;
    }
};

递归法

class Solution {
public:
    int minDepth(TreeNode* root) {
        if(root == nullptr){
            return 0;
        }
        int leftMin = minDepth(root->left);
        int rightMin = minDepth(root->right);

        if(root->left == nullptr && root->right != nullptr){
            return 1 + rightMin;
        }
        if(root->right == nullptr && root->left != nullptr){
            return 1 + leftMin;
        }
        //陷阱:不能直接从max改成min,考虑有一方全部为空的情况,形状像链状的二叉树

        return min(leftMin,rightMin) + 1;

    }
};

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

leetcode链接:力扣题目链接

视频链接:要理解普通二叉树和完全二叉树的区别! | LeetCode:222.完全二叉树节点的数量 (opens new window)

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

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

示例 1:

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

输入:root = []
输出:0
示例 3:

输入:root = [1]
输出:1

image

首先我的思路是用层序遍历,每次元素出队时count++,因为每个元素只会出队一次。感觉适合所有的二叉树:

class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> q;
        int count = 0;
        if(root == nullptr){
            return count;
        }
        q.push(root);
        while(!q.empty()){
            unsigned int sz = q.size();
            for(int i = 0; i < sz; i++){
                TreeNode* cur = q.front();
                q.pop();
                count++;
                if(cur->left != nullptr){
                    q.push(cur->left);
                }
                if(cur->right != nullptr) {
                    q.push(cur->right);
                }
            }
        }
        return count;
    }
};

最后确实是通过了,但没有利用好完全二叉树的条件,是否有更巧妙的方法?

是有的!

完全二叉树只有两种情况:

  • 情况一:就是满二叉树
  • 情况二:最后一层叶子节点没有满。

对于情况一,可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1。

对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。

判断其子树是不是满二叉树,如果是则利用公式计算这个子树(满二叉树)的节点数量,如果不是则继续递归,那么 在递归三部曲中,第二部:终止条件的写法应该是这样的:

if (root == nullptr) return 0; 
// 开始根据左深度和右深度是否相同来判断该子树是不是满二叉树
TreeNode* left = root->left;
TreeNode* right = root->right;
int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
while (left) {  // 求左子树深度
    left = left->left;
    leftDepth++;
}
while (right) { // 求右子树深度
    right = right->right;
    rightDepth++;
}
if (leftDepth == rightDepth) {
    return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,返回满足满二叉树的子树节点数量
}

递归三部曲,第三部,单层递归的逻辑:(可以看出使用后序遍历)

int leftTreeNum = countNodes(root->left);       // 左
int rightTreeNum = countNodes(root->right);     // 右
int result = leftTreeNum + rightTreeNum + 1;    // 中
return result;

可以得到最终的代码:

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr) return 0;
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
        while (left) {  // 求左子树深度
            left = left->left;
            leftDepth++;
        }
        while (right) { // 求右子树深度
            right = right->right;
            rightDepth++;
        }
        if (leftDepth == rightDepth) {
            return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
        }
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};

总结

迭代算法适合BFS求最小最短,递归代码简洁,但比较抽象,希望可以好好锻炼递归思维。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值