题目:
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
解答:
1 public calss Solution { 2 3 public int minDepth(TreeNode root) { 4 if(root == null) { 5 return 0; 6 } 7 8 if(root.left == null) { 9 return minDepth(root.right); 10 } 11 12 if(root.right == null) { 13 return minDepth(root.left); 14 } 15 16 return Math.min(minDepth(root.left), minDepth(root.right)) + 1; 17 } 18 }