Leetcode 111, Minimum Depth of Binarytree, 难度easy
这道题跟112是一样的,只要稍微的改一下就可以了,也就是在每一次递归时将层数加一,最后对比左子树和右子树的层数,将小的层数返回。
class Solution {
public:
int minDepth(TreeNode* root) {
int res = 0;
if(root == NULL) return 0;
return DFS(root, res);
}
int DFS(TreeNode* root, int res) {
res++;
int left = root -> left == NULL? (root -> right == NULL? res: INT_MAX) : DFS(root -> left, res);
int right = root -> right == NULL? (root -> left == NULL? res: INT_MAX) : DFS(root -> right, res);
if(left < right) return left;
return right;
}
};