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

    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.

    思路:

      dfs

    我的代码:

    public class Solution {
        public int minDepth(TreeNode root) {
            if(root == null)    return 0;
            if(root.left == null && root.right == null) return 1;
            int left = Integer.MAX_VALUE;
            int right = Integer.MAX_VALUE;
            if(root.left != null)
                left = minDepth(root.left);
            if(root.right != null)
                right = minDepth(root.right);
            return Math.min(left, right) + 1;
        }
    }
    View Code
  • 相关阅读:
    053587
    053586
    053585
    053584
    053583
    053582
    053581
    053580
    053579
    053578
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4337698.html
Copyright © 2011-2022 走看看