zoukankan      html  css  js  c++  java
  • 111. Minimum Depth of Binary Tree

    题目:

    Given a binary tree, find its minimum depth.

    The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

    Hide Tags
     Tree Depth-first Search 

    链接: http://leetcode.com/problems/minimum-depth-of-binary-tree/

    题解:

    求二叉树最短路径长度。依然使用DFS递归求解。

    Time Complexity - O(n), Space Complexity - O(n)。 

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

    Update:

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int minDepth(TreeNode root) {
            if(root == null)
                return 0;
            if(root.left == null)
                return 1 + minDepth(root.right);
            else if(root.right == null)
                return 1 + minDepth(root.left);
            else
                return 1 + Math.min(minDepth(root.left), minDepth(root.right));
        }
    }

    二刷:

    要注意题目规定最短路径必须是非空leaf node,所以我们要对root.left 或者root.right为空时进行判断。  这道题目也可以用BFS层序遍历来做,这样的话可以避免很多不必要的查找。

    Java:

    Recursive:

    Time Complexity - O(n), Space Complexity - O(n)。 

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int minDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            if (root.left == null) {
                return 1 + minDepth(root.right);
            } else if (root.right == null) {
                return 1 + minDepth(root.left);
            } else {
                return 1 + Math.min(minDepth(root.left), minDepth(root.right));    
            }
        }
    }

    三刷:

    Java:

    DFS:

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int minDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            if (root.left == null) {
                return 1 + minDepth(root.right);
            } else if (root.right == null) {
                return 1 + minDepth(root.left);
            } else {
                return 1 + Math.min(minDepth(root.left), minDepth(root.right));    
            }
        }
    }

    BFS:

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int minDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            Queue<TreeNode> q = new LinkedList<>();
            q.offer(root);
            int curLevel = 1, nextLevel = 0;
            int depth = 1;
            
            while (!q.isEmpty()) {
                TreeNode node = q.poll();
                curLevel--;
                if (node.left == null && node.right == null) {
                    return depth;
                }
                if (node.left != null) {
                    q.offer(node.left);
                    nextLevel++;
                } 
                if (node.right != null) {
                    q.offer(node.right);
                    nextLevel++;
                } 
                if (curLevel == 0) {
                    curLevel = nextLevel;
                    nextLevel = 0;
                    depth++;
                }
            }
            return depth;
        }
    }

    Reference:

    https://leetcode.com/discuss/25060/my-4-line-java-solution

    https://leetcode.com/discuss/61476/bfs-c-8ms-beats-99-94%25-submissions

  • 相关阅读:
    一步一步学习开发BPM工作流系统(三)开发WinForm的应用平台1
    支持多数据库本地和远程调用的数据访问层架构
    HF Web表单开发技术文档
    CDZSC_2015寒假新人(2) 数学 C
    CDZSC_2015寒假新人(2)——数学 A
    ZSC 1306: 沼跃鱼早已看穿了一切 题解
    解决”java.lang.UnsatisfiedLinkError: Native Library .dll already loaded in another classloader”的问题
    有目标就要坚持
    (转)新兴XML处理方法VTDXML介绍
    (转)Java远程通讯可选技术及原理
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4437326.html
Copyright © 2011-2022 走看看