111. Minimum Depth of Binary Tre

2017-01-07  本文已影响0人  juexin

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.

public class Solution {
    public int minDepth(TreeNode root) {
        if(root == null)
          return 0;
        int Left = minDepth(root.left);
        int Right = minDepth(root.right);
        
        if(Left==0&&Right==0)
          return 1;
        if(Left==0)
          Left = Integer.MAX_VALUE;
        if(Right==0)
          Right = Integer.MAX_VALUE;
        
        return (Left>Right)? (Right+1):(Left+1);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读