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)。

浙公网安备 33010602011771号