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

 

解答:

 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 }

 

posted @ 2019-02-22 15:47  林木声  阅读(108)  评论(0编辑  收藏  举报