代码随想录算法训练营第十六天 _ 二叉树_104.二叉树的最大深度 559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数。

学习目标:

60天训练营打卡计划!

学习内容:

104.二叉树的最大深度

  • 学到了一个新思想,分树(左右两棵子树)行动,思路很类似于对称二叉树!
  • 递归要有目的。此处是return max + 1;。
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        // 后序遍历
        int leftNum = maxDepth(root.left);
        int rightNum = maxDepth(root.right);
        int max = Math.max(leftNum, rightNum);
        // 必须要+1,否则该递归毫无意义
        return max + 1;

    }
}

// 层序遍历
class Solution {
    public int maxDepth(TreeNode root) {
        Queue<TreeNode> que = new LinkedList<>();
        int size = -1;
        int res = 0;
        if(root == null) return res;
        que.add(root);
        while(!que.isEmpty()){
            size = que.size();
            res++;
            while(size-- > 0){
                TreeNode node = que.remove();
                if(node.left != null) que.add(node.left);
                if(node.right != null) que.add(node.right);
            }
        }
        return res;
    }
}

559.n叉树的最大深度

  • 只求最大值,这个思路很棒。
  • 否则需要创建一个数组来存储各个节点的最大值,后续还需要再比较。
class Solution {
    public int maxDepth(Node root) {
        if(root == null)  return 0;
        int max = 0;
        if(root.children != null){
            for(Node child : root.children){
                // 只需要最大值,就把当前的值和最大值作比较即可。
                max =  Math.max(max,maxDepth(child));
            }
        }
        return max + 1;

    }
}

class Solution {
    public int maxDepth(Node root) {
        Queue<Node> que = new LinkedList<>();
        int size = -1;
        int res = 0;
        if(root == null) return res;
        que.add(root);
        while(!que.isEmpty()){
            size = que.size();
            res++;
            while(size-- > 0){
                Node node = que.remove();
                // if(node.children != null){
                //     for(Node child : node.children){
                //         if(child != null) que.add(child);
                //     }
                // }
                // 两个循环都可以
                for (int i = 0; i < node.children.size(); i++)
                    if (node.children.get(i) != null) 
                        que.offer(node.children.get(i));
            }
        }
        return res;
    }
}

111. 二叉树的最小深度

  • 总的逻辑和最大深度差别不大
  • 迭代法对左右子树各自为空的情况的讨论没有区分的很好。
  • 层序遍历时:
    • 一定要明确一个前提:
      最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
      说明:叶子节点是指没有子节点的节点。
      突破口就是叶子节点是没有子节点的节点,层序遍历是从左到右遍历的。
      第一次遇到没有子节点的节点时一定就是最小的深度。
class Solution {
    public int minDepth(TreeNode root) {
        if(root == null)   return 0;

        int minLeft = minDepth(root.left);
        int minRight = minDepth(root.right);

        if(root.left == null && root.right != null){
            return 1 + minRight;
        }
        if(root.left != null && root.right == null){
            return 1 + minLeft;
        }

        return Math.min(minLeft, minRight) + 1;
    }
}

// 层序遍历 yyds
class Solution {
    public int minDepth(TreeNode root) {
        Queue<TreeNode> que = new LinkedList<>();
        int size = 0;
        int depth = 0;
        int flag = 0;
        if(root == null) return 0;
        
        que.add(root);
        while(!que.isEmpty()){
            size = que.size();
            depth++;
            while(size-- > 0){
                TreeNode node = que.remove();
                if(node.left != null)  que.add(node.left);
                if(node.right != null)  que.add(node.right);
                if(node.left == null && node.right == null){
                    flag++;
                    break;
                }
            }
            if(flag == 1)
                break;
        }
        return depth;

    }
}

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

  • 总的逻辑和最大深度差别不大
//层序遍历
class Solution {
    public int countNodes(TreeNode root) {
        Queue<TreeNode> que = new LinkedList<>();
        int size = -1;
        int res = 0;
        if(root == null)  return res;

        que.add(root);
        while(!que.isEmpty()){
            size = que.size();
            while(size-- > 0){
                TreeNode node = que.remove();
                res++;
                if(node.left != null)  que.add(node.left);
                if(node.right != null)  que.add(node.right);
            }
        }

        return res;
    }
}

// 后序遍历
class Solution {
    public int countNodes(TreeNode root) {
        if(root == null)  return 0;
        int leftNum = countNodes(root.left);
        int rightNum = countNodes(root.right);

        int res = leftNum + rightNum + 1;

        return res;
    }
}

// 针对满二叉树的特性设计的算法
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(leftDepth == rightDepth)
            // 四则运算的优先级高于位运算!!
            return (2 << leftDepth) - 1;
        
        int leftNum = countNodes(root.left);
        int rightNum = countNodes(root.right);
        int res = leftNum + rightNum + 1;

        return res;
    }
}

学习时间:

  • 上午一小时,下午两个半小时,晚上一个半小时。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值