101. Symmetric Tree

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if (!root) return true;
        return (helper(root->left, root->right));
    }

    bool helper(TreeNode* rootL, TreeNode* rootR) {
        if (!rootL && rootR) return false;
        if (rootL && !rootR) return false;
        if (!rootL && !rootR) return true;
        if (rootL->val != rootR->val) return false;
        return helper(rootL->right, rootR->left) && helper(rootR->right, rootL->left);
    }
};

results matching ""

    No results matching ""