海智算法训练营第十七天 | 第六章二叉树 part03 | 104.二叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数

文章介绍了如何使用递归算法求解二叉树的最大深度和最小深度,以及针对完全二叉树的节点个数计算方法,包括层序、后序和前序遍历的应用。
摘要由CSDN通过智能技术生成

1.熟悉使用递归求二叉树的最大和最小深度

2.学习完全二叉树求节点个数的特殊求法

1.二叉树的最大深度​​​​​​​

题目:二叉树的最大深度

1.层序遍历法,在之前的bfs题目里做过,就不细琐了,就是典型的bfs模板

class Solution {
    public int maxDepth(TreeNode root) {
        int res = 0;
        if(root == null) return res;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            List<Integer> ans = new ArrayList<>();
            int size = queue.size();
            res ++;
            while(size -- > 0){
                TreeNode temp = queue.poll();
                ans.add(temp.val);
                if(temp.left != null) queue.add(temp.left);
                if(temp.right != null) queue.add(temp.right);
            }
        }
        return res;

    }
}

 2.递归法,后序遍历、前序也可以,但是后序代码更好写一些,前序的需要有回溯操作,因为是中左右,执行完左之后需要回溯成中的样子再执行右。

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

这是后序递归的样子

题目:N叉树的最大深度

1.递归法

class Solution {
    public int maxDepth(Node root) {
        if(root == null) return 0;
        int max = 0;
        for(Node temp: root.children){
            max = Math.max(maxDepth(temp),max);
        }
        return max + 1;
    }
}

2.层序遍历一个道理就不重复写了

2.二叉树的最小深度

1.后序遍历递归法:不能直接使用求最大深度的算法,因为有可能左子树或者右子树为空,这时候并不是叶子节点,但是返回的确是0。

class Solution {
    public int minDepth(TreeNode root) {
        if(root == null) return 0;
        if(root.left == null) return 1+minDepth(root.right);//左子树为空
        if(root.right == null) return 1+minDepth(root.left);//右子树为空
        //两者都不为空
        return 1+Math.min(minDepth(root.left) , minDepth(root.right));
    }
}

2.前序遍历法

class Solution {
    /**
     * 递归法(思路来自二叉树最大深度的递归法)
     * 该题求最小深度,最小深度为根节点到叶子节点的深度,所以在迭代到每个叶子节点时更新最小值。
     */
    int depth = 0;
    // 定义最小深度,初始化最大值
    int minDepth = Integer.MAX_VALUE;
    public int minDepth(TreeNode root) {
        dep(root);
        return minDepth == Integer.MAX_VALUE ? 0 : minDepth;
    }
    void dep(TreeNode root){
        if(root == null) return ;
        // 递归开始,深度增加
        depth++;
        dep(root.left);
        dep(root.right);
        // 该位置表示递归到叶子节点了,需要更新最小深度minDepth
        if(root.left == null && root.right == null)
            minDepth = Math.min(minDepth , depth);
        // 递归结束,深度减小
        depth--;
    }
}

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

题目:完全二叉树的节点个数

这里就不用传统二叉树的做法了,和之前层序遍历章节做的大差不差

首先要明白完全二叉树的定义:

思路:完全二叉树向下递归,每到一个节点就判断他的左深度是不是和右深度相同,如果相同那么这个节点就是一个完全二叉树的根节点,然后再返回到上一层,就不用接着往下遍历了

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;
        while(left != null){
            left = left.left;
            leftDepth++;
        }
        while(right != null){
            right = right.right;
            rightDepth++;
        }

        if(leftDepth == rightDepth) return (2 << leftDepth) -1 ;
        return countNodes(root.left) + countNodes(root.right) +1;
//如果不是完全二叉树接着往下遍历

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值