111. Minimum Depth of Binary Tree

注意只有一个子节点的情况,分开讨论

 

 1 class Solution {
 2     public int minDepth(TreeNode root) {
 3         if(root == null) return 0;
 4         if(root.left != null && root.right == null) {
 5             return(minDepth(root.left) + 1);
 6         }else if(root.left == null && root.right != null) {
 7             return(minDepth(root.right) + 1);
 8         }
 9         return(Math.min(minDepth(root.left), minDepth(root.right)) + 1);
10            
11     }
12 }

 

posted @ 2018-09-21 08:02  jasoncool1  阅读(108)  评论(0编辑  收藏  举报