Simple C++ solution, 8ms


  • 0
    V
    class Solution {
    public:
        bool helper(TreeNode *rl, TreeNode *rr){
            if(!rl && !rr) // both null
                return true;
            if(!rl || !rr || rl -> val != rr->val) // value not symmetric 
                return false;
            TreeNode * rll = rl->left;
            TreeNode * rlr = rl->right;
            TreeNode * rrl = rr->left;
            TreeNode * rrr = rr->right;
            return helper(rll,rrr) && helper(rlr,rrl);
        }
        bool isSymmetric(TreeNode *root) {
            TreeNode *rl = root;
            TreeNode *rr = root;
            return helper(rl,rr);
        }
    };
    

    used recursion.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.