[leetcode]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.
跟[leetcode]Maximum Depth of Binary Tree 差不多。
递归,不多说。
1 public class Solution { 2 public int minDepth(TreeNode root) { 3 if(root == null) return 0; 4 if(root.left == null && root.right == null) return 1; 5 int left = Integer.MAX_VALUE,right = Integer.MAX_VALUE; 6 if(root.left != null) left = minDepth(root.left); 7 if(root.right != null) right = minDepth(root.right); 8 return left > right ? right + 1 : left +1; 9 } 10 }