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

  • 相关阅读:
    javascript04 引用类型
    apache httpd.conf 配置信息
    服务器集群原理
    网站优化常用技术
    php垃圾回收
    smarty2 简单应用 小留言板
    C# DataGridView 编辑单元格保存时防止单元格在编辑状态而产生空值
    C# 把字符串类型日期转换为日期类型
    C# Split方法操作取出文本文档txt文件里数据
    WinForm OR ASP.NET获取修改配置文件config的节点
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4437326.html
Copyright © 2011-2022 走看看