【代码随想录算法训练营第16天| 104.二叉树的最大深度,111.二叉树的最小深度,222.完全二叉树的节点个数】

一. 二叉树相关算法题

104.二叉树的最大深度

什么是二叉树的深度与高度?

  • 二叉树当前节点的深度:是指从根节点到该节点的最长路径边的条数或者节点数
  • 二叉树当前节点的高度:是指从该节点到叶子节点最长简单路径的条数或节点数

后序遍历-递归

思路
  • 此题可以用前序或则后续遍历计算
  • 可以简化模型为一个三个节点的两层二叉树思考,那么子需要计算根节点到左右叶子节点的距离最大值加上根节点那一层就是最大深度
  • 递归三要素:
    • 递归入参出参:入参,子树根节点;出参,长度
    • 终止条件:如果当前节点为空那么当前层深度为0if(node==null) return 0;
    • 单层递归逻辑**(后序左右中)**:先求左叶子节点深度再求右叶子节点深度然后取最大值加1,代码如下
int leftDepth = getDepth(node.left);
int rightDepth = getDepth(node.right);
int curDepth = 1+ Math.max(leftDepth,rightDepth);
return curDepth;

整体代码如下:

class Solution {
  public int maxDepth(TreeNode root) {
    return getDepth(root);
  }
  public int getDepth(TreeNode cur){
      if (cur == null) return 0;
      int leftDepth = getDepth(cur.left);
      int rightDepth = getDepth(cur.right);
      int curDepth = 1+ Math.max(leftDepth,rightDepth);
      return curDepth;
      
  }
}

简化后的代码如下:

class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        return 1 + Math.max(maxDepth(root.left),maxDepth(root.right));
    }
}

前序遍历-递归

思路
  • 回溯–二刷再看

111.二叉树的最小深度

递归实现

思路
  • 搜先明确题目中是求出到叶子节点的最小深度,所以最小深度对应的节点的左右叶子都为空才算如图所示
    在这里插入图片描述

  • 由此在一题最大深度的基础上调整单层处理逻辑

class Solution {
    public int minDepth(TreeNode root) {
        return getMinDepth(root);
    }

    public int getMinDepth(TreeNode cur){
        if (cur == null) return 0;
        int left = getMinDepth(cur.left);
        int right = getMinDepth(cur.right);
        // 左右叶子节点都为空的节点才算数所以有如下三种情况
        // 如果左叶子节点不为空右叶子节点为空,那么计算左叶子节点对应的最小深度
        if (cur.left != null && cur.right == null) {
            return 1+left;
        }
        // 如果右叶子节点不为空,且左叶子节点为空,那么查找右叶子节点的最小深度并+1返回
        if (cur.right != null && cur.left == null) {
            return 1+right;
        }
        // 如果都为空返回最小值
        return 1+Math.min(left,right);
    }
}

222.完全二叉树节点个数

递归遍历

思路
  • 后序递归遍历
  • 入参–出参:当前节点–累加节点数
  • 递归终止条件:节点为空
  • 每层递归执行逻辑:当前节点加左右节点
if (root == null) return 0;
int left = countNodes(root.left);
int right = countNodes(root.right);
return 1+left+right;
  • 具体代码如下
class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        int left = countNodes(root.left);
        int right = countNodes(root.right);
        return 1+left+right;
    }
}
  • 时间复杂度:O(n) ,通过Master公式计算得出
  • 空间复杂度:O(nlogn)

层序遍历-迭代

  • 在层序遍历的基础上计算节点数就可以了
class Solution {
    public int countNodes(TreeNode root) {
        Deque<TreeNode> deque = new LinkedList<>();
        if (root!= null) deque.push(root);
        // 总结点数
        int sum = 0;
        while (!deque.isEmpty()) {
            int size = deque.size();
            //累计每层节点数
            sum+=size;
            while (size-->0){
                TreeNode node = deque.pollLast();
                if (node.left != null) deque.push(node.left);
                if (node.right != null) deque.push(node.right);
            }
        }
        return sum;
    }
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(n)

通过完全二叉树的特性求出

  • 首先明确两点
    1. 满二叉树的节点个数可以通过公式计算2^深度-1;
    2. 完全二叉树末端一定是由多个满二叉树组成,如图所示
      在这里插入图片描述

在这里插入图片描述

  • 综上可以利用二叉树末端一定是多个满二叉树组成并且满二叉树只需要直到深度就可以计算节点数的特性节本题
    1. 遍历左右子树:只向左,只向右遍历如果最后两边的深度相等那么就是满二叉子树,计算节点数+1返回
    2. 如果不等递归遍历子树
class Solution {
    public int countNodes(TreeNode root) {
        // 递归终止条件
        {
            if (root == null) return 0;

            // 子树左右深度
            int leftDepth = 0;
            int rightDepth = 0;
            TreeNode left = root.left;
            TreeNode right = root.right;
            while (left != null){
                left = left.left;
                leftDepth++;
            }
            while (right != null){
                right = right.right;
                rightDepth++;
            }
            // 如果是满二叉树返回二叉子树节点数
            if (rightDepth==leftDepth){
                return (2<<leftDepth)-1;
            }
        }

        //单层递归逻辑
        int left = countNodes(root.left);
        int right = countNodes(root.right);
        return 1+left+right;
    }
}
  • 时间复杂度O(logn × logn)
  • 空间复杂度O(logn)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值