当前位置 博文首页 > Yawn:Leetcode——二叉树的最大/小深度

    Yawn:Leetcode——二叉树的最大/小深度

    作者:[db:作者] 时间:2021-08-02 15:47

    1.1 最大深度

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

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

    说明: 叶子节点是指没有子节点的节点。
    在这里插入图片描述

    1.2 题解

    解法一:

    递归

    在这里插入图片描述

    深度优先搜索:

    如果我们知道了左子树和右子树的最大深度 ll 和 rr,那么该二叉树的最大深度即为

    • max(l,r)+1

    而左子树和右子树的最大深度又可以以同样的方式进行计算。因此我们可以用「深度优先搜索」的方法来计算二叉树的最大深度。具体而言,在计算当前二叉树的最大深度时,可以先递归计算出其左子树和右子树的最大深度,然后在 O(1)时间内计算出当前二叉树的最大深度。递归在访问到空节点时退出。

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

    解法三:

    广度优先搜索:

    我们也可以用「广度优先搜索」的方法来解决这道题目,但我们需要对其进行一些修改,此时我们广度优先搜索的队列里存放的是「当前层的所有节点」。每次拓展下一层的时候,不同于广度优先搜索的每次只从队列里拿出一个节点,我们需要将队列里的所有节点都拿出来进行拓展,这样能保证每次拓展完的时候队列里存放的是当前层的所有节点,即我们是一层一层地进行拓展,最后我们用一个变量 ans 来维护拓展的次数,该二叉树的最大深度即为ans。

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        public int maxDepth(TreeNode root) {
            if(root == null)
                return 0;
            Queue<TreeNode> queue =new LinkedList<TreeNode>();
            int ans = 0;
            queue.offer(root);          //将根节点存入队列
            while(!queue.isEmpty()){
                int size = queue.size();      //计算当前层节点个数
                while(size > 0){
                    TreeNode node = queue.poll();  //取出该层每一个节点,并将该层子节点存入队列,为下一层遍历做准备
                    if(node.left != null)
                        queue.offer(node.left);
                    if(node.right != null)
                        queue.offer(node.right);
                    size--;                        //该层下一个节点
                }
                ans++;
            }
            return ans;
        }
    }
    

    2.1 最小深度

    在这里插入图片描述

    2.2 题解

    递归:

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int minDepth(TreeNode root) {
            if(root == null) {		//为空直接返回
                return 0;
            }
            if(root.left == null && root.right == null) {	//只有根节点时
                return 1;
            }
            int ans = Integer.MAX_VALUE;
            if(root.left != null) {				//左子树深度
                ans = Math.min(minDepth(root.left), ans);
            }
            if(root.right != null) {			//右子树深度
                ans = Math.min(minDepth(root.right), ans);
            }
            return ans + 1;						//最终深度
        }
    }
    

    迭代:

    • 注意这个条件
    • // 是叶子结点,直接返回depth,因为从上往下遍历,所以该值就是最小值
    class Solution {
       /**
         * 迭代法,层序遍历
         */
        public int minDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            Deque<TreeNode> deque = new LinkedList<>();
            deque.offer(root);
            int depth = 0;
            while (!deque.isEmpty()) {
                int size = deque.size();
                depth++;
                for (int i = 0; i < size; i++) {
                    TreeNode poll = deque.poll();
                    
                    // 是叶子结点,直接返回depth,因为从上往下遍历,所以该值就是最小值
                    if (poll.left == null && poll.right == null) {
                        return depth;
                    }
                    if (poll.left != null) {
                        deque.offer(poll.left);
                    }
                    if (poll.right != null) {
                        deque.offer(poll.right);
                    }
                }
            }
            return depth;
        }
    }
    
    
    cs
    下一篇:没有了