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

二叉树的深度和高度

在这里插入图片描述

104.二叉树的最大深度

在这里插入图片描述

思路:

递归法

本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。

**二叉树节点的深度:**指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
**二叉树节点的高度:**指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)
而根节点的高度就是二叉树的最大深度,所以本题中我们通过后序求的根节点高度来求的二叉树最大深度。

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

    }
}

迭代法-层序遍历

class Solution {
    public int maxDepth(TreeNode root) {
        // List<Integer>res =new ArrayList<>();
        Queue<TreeNode>queue =new LinkedList<>();
        if(root!=null){
            queue.add(root);
        }
        int deep=0;
        while(!queue.isEmpty()){
            deep++;
            int len=queue.size();
            for(int i=0;i<len;i++){
                TreeNode node = queue.poll();
                if(node.left!=null)queue.add(node.left);
                if(node.right!=null)queue.add(node.right);
            }
        }
        return deep;
    }
}

559. N 叉树的最大深度

在这里插入图片描述

思路一-迭代法

class Solution {
    public int maxDepth(Node root) {
         // List<Integer>res =new ArrayList<>();
         Queue<Node>queue =new LinkedList<>();
         if(root!=null){
             queue.add(root);
         }
         int deep=0;
         while(!queue.isEmpty()){
             deep++;
             int len=queue.size();
             for(int i=0;i<len;i++){
                 Node node = queue.poll();
                //  if(node.left!=null)queue.add(node.left);
                //  if(node.right!=null)queue.add(node.right);
                for(Node child:node.children){
                    if(child!=null){
                        queue.add(child);
                    }
                }
             }
         }
         return deep;
     
    }
}

思路二-递归法

class Solution {
    public int maxDepth(Node root) {
        // List<Integer>res =new ArrayList<>();
        if (root == null)
            return 0;
        int deep = 0;
        if (root.children != null) {
            for (Node child : root.children) {
                deep = Math.max(maxDepth(child), deep);
            }
        }
        return deep + 1;
    }
}

111. 二叉树的最小深度

在这里插入图片描述

思路1-迭代

遇到两个节点都为null时,直接返回深度即可

class Solution {
    public int minDepth(TreeNode root) {
        // List<Integer>res =new ArrayList<>();
         Queue<TreeNode>queue =new LinkedList<>();
         if(root!=null){
             queue.add(root);
         }
         int deep=0;
         while(!queue.isEmpty()){
             deep++;
             int len=queue.size();
             for(int i=0;i<len;i++){
                 TreeNode node = queue.poll();
                 if(node.left!=null)queue.add(node.left);
                 if(node.right!=null)queue.add(node.right);
                 if(node.left==null&&node.right==null){
                    return deep;
                 }
             }
         }
         return deep;
    }
}

思路2-递归-最小深度比最大深度的递归多了空左右孩子的判断

所以,如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。

反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。 最后如果左右子树都不为空,返回左右子树深度最小值 + 1 。
遍历的顺序为后序(左右中),可以看出:求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑。

class Solution {
    public int minDepth(TreeNode root) {
        int deep=0;
        if(root==null)return 0;
        int left=minDepth(root.left);
        int right=minDepth(root.right);
        if(root.left==null){
            return right+1;
        }
        if(root.right==null){
            return left+1;
        }
        int height=Math.min(left,right);
        return height+1;
    }
}

222.完全二叉树的节点个数【无思路】

在这里插入图片描述

思路一:二叉树的递归和迭代

递归

class Solution {
    // 通用递归解法
    public int countNodes(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}

迭代:

class Solution {
    // 迭代法
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int result = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size -- > 0) {
                TreeNode cur = queue.poll();
                result++;
                if (cur.left != null) queue.offer(cur.left);
                if (cur.right != null) queue.offer(cur.right);
            }
        }
        return result;
    }

思路二:判断完全二叉树

在这里插入图片描述

  • 完全二叉树1
  • 在这里插入图片描述
  • 完全二叉树2
  • 在这里插入图片描述

如何判断是否满足完全二叉树?

在这里插入图片描述

递归三部曲

第一步:确定输入和返回值
第二部:确定终止函数

  1. 空节点返回0个结点
  2. 当为完全二叉树时,返回完全二叉树的节点个数

第三部:确定单层递归的逻辑:后续遍历
在这里插入图片描述

代码:

class Solution {
    public int countNodes(TreeNode root) {
        if(root==null)return 0;
        TreeNode left=root.left;
        TreeNode right=root.right;
        int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
        while(left!=null){//一直查找到最小的节点
            left=left.left;
            leftDepth++;
        }
        while(right!=null){
            right=right.right;
            rightDepth++;
        }
        // 代表是完全二叉树
        if(leftDepth == rightDepth){
            return (2<<leftDepth)-1;//相当于2^4-1
        }
        // int leftdepth=countNodes(root.left);
        // int rightdepth=countNodes(root.right);
        return countNodes(root.left)+countNodes(root.right)+1;
    }
}
  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值