[leetcode] Minimum Depth of Binary Tree, Maximum Depth of Binary Tree
[leetcode] Minimum Depth of Binary Tree
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root) return 0;
if(!root->left) return minDepth(root->right) + 1;
if(!root->right) return minDepth(root->left) + 1;
return min(minDepth(root->left), minDepth(root->right)) + 1;
}
};
[leetcode] Maximum Depth of Binary Tree
class Solution {
public:
int maxDepth(TreeNode* root) {
if(!root) return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};