코테준비 - Balanced Binary Tree

정상화·2023년 2월 26일

LeetCode

목록 보기
107/222

Balanced Binary Tree

class Solution {
public:
    bool isBalanced(TreeNode *root) {
        return root == nullptr
               || isBalanced(root->left)
               && isBalanced(root->right)
               && (abs(height(root->left) - height(root->right)) < 2);
    }

    int height(TreeNode *node) {
        if (node == nullptr) {
            return 0;
        }
        return max(height(node->left), height(node->right)) + 1;
    }
};
profile
백엔드 희망

0개의 댓글