Leetcode 101. Symmetric Tree

smpl published on
1 min, 68 words

문제 : 101. Symmetric Tree

등급 : Easy

class Solution {
    bool compare(TreeNode* leftRoot, TreeNode* rightRoot) {
        if (!leftRoot && !rightRoot) {
            return true;
        }
        
        if ((!leftRoot && rightRoot) || (leftRoot && !rightRoot)) {
            return false;
        }
        
        if (leftRoot->val != rightRoot->val) {
            return false;
        }
        
        if (!compare(leftRoot->left, rightRoot->right)) {
            return false;
        }
        
        return compare(leftRoot->right, rightRoot->left);
    }
    
public:
    bool isSymmetric(TreeNode* root) {
        if (!root) { 
            return true;
        }
        
        return compare(root->left, root->right);
    }
};