zoukankan      html  css  js  c++  java
  • Minimum Depth of Binary Tree 解答

    Question

    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.

    Solution 1 -- Recursion

    Problems related with tree can always be solved by recursion. We need only consider three situations:

    1. parent node

    2. left child node

    3. right chile node

    Time complexity O(n), space cost O(1).

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public int minDepth(TreeNode root) {
    12         if (root == null)
    13             return 0;
    14         if (root.left == null)
    15             return minDepth(root.right) + 1;
    16         if (root.right == null)
    17             return minDepth(root.left) + 1;
    18         return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
    19     }
    20 }

    Solution 2 -- Iteration

    We can also solve this problem by visiting the tree level by level. Time complexity O(n), space cost O(n).

     Notice that using ArrayList will save almost half time compared with using ArrayDeque. Actually, we need not to pop staff here.

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public int minDepth(TreeNode root) {
    12         if (root == null)
    13             return 0;
    14         List<TreeNode> current = new ArrayList<TreeNode>();
    15         List<TreeNode> next;
    16         current.add(root);
    17         int result = 1;
    18         while (current.size() > 0) {
    19             next = new ArrayList<TreeNode>();
    20             int length = current.size();
    21             for (TreeNode tmpNode : current) {
    22                 // If tmpNode is leaf node
    23                 if (tmpNode.left == null && tmpNode.right == null)
    24                     return result;
    25                 if (tmpNode.left != null)
    26                     next.add(tmpNode.left);
    27                 if (tmpNode.right != null)
    28                     next.add(tmpNode.right);
    29             }
    30             current = next;
    31             result++;
    32         }
    33         return result;
    34     }
    35

    Discussion

    Recursion is not always the time consuming solution. For example, in this problem, every node has been calculated height for only once, so there's no redundance. However, for Fibonacci problem, when we calculate F(5), we get F(5) = F(4) + F(3), which is redundant calculation. Under this circumtance, we should use Dynamic Programming.

  • 相关阅读:
    Hive系统函数之collect_list和collect_set
    SpringBoot集成JWT实现token验证
    Hadoop添加LZO压缩支持
    常见函数封装(自用,随时更新)
    vs-code 的常用插件
    在移动端显示控制台信息
    SublimeText 3 常见快捷键
    vue的自定义指令
    修改滚动条样式
    vue、react中循环遍历为什么会有key,key有什么作用?
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/4834089.html
Copyright © 2011-2022 走看看