non-recursive simple JavaScript solution


  • 0
    J
    var isSymmetric = function(root) {
        var stack1 = [root],
            stack2 = [root];
        var cur1, cur2;
        while (stack1.length > 0) {
            cur1 = stack1.pop();
            cur2 = stack2.pop();
            if (!cur1 && !cur2) continue;
            if (!cur1 || !cur2) return false;
            if (cur1.val !== cur2.val) return false;
            stack1.push(cur1.right);
            stack1.push(cur1.left);
            stack2.push(cur2.left);
            stack2.push(cur2.right);
        }
        return true;
    };
    

Log in to reply
 

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