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.

Minimum Depth of Binary Tree
 1 public class Solution {
 2     public int minDepth(TreeNode root) {
 3         if(root==null) return 0;
 4         int left = minDepth(root.left);
 5         int right = minDepth(root.right);
 6         if(left==0 && right ==0) return 1;
 7         if(left ==0) left=Integer.MAX_VALUE;
 8         if(right==0) right=Integer.MIN_VALUE;
 9         return Math.min(right,left)+1;
10     }
11 }
View Code

Minimum Depth of Binary Tree

上一篇:uva 10816(二分+Dijkstra)


下一篇:Remove Duplicates from Sorted List