최대 1 분 소요

[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;
    }
};

카테고리:

업데이트: