LeetCode 111. Minimum Depth of Binary Tree

1 class Solution {
2 public:
3     int minDepth(TreeNode* root) {
4         if(root == NULL) return 0;
5         if(root->left == NULL || root->right == NULL) return 1 + max(minDepth(root->left),minDepth(root->right));
6         return 1 + min(minDepth(root->left),minDepth(root->right));
7     }
8 };

 第5行:以防出现某个节点只有一个儿子的情况(如果出现这种情况但还是return 1+min,则实际上是return 1+0)。

posted @ 2016-02-23 23:09  co0oder  阅读(111)  评论(0编辑  收藏  举报