美文网首页
111. Minimum Depth of Binary Tre

111. Minimum Depth of Binary Tre

作者: juexin | 来源:发表于2017-01-07 18:37 被阅读0次

    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);
        }
    }
    

    相关文章

      网友评论

          本文标题:111. Minimum Depth of Binary Tre

          本文链接:https://www.haomeiwen.com/subject/bzvfbttx.html