non-recursive javascript solution using queue


  • 0
    L
    var isSymmetric = function(root) {
        if(root === null) return true;
        let queL = [], queR = [];
        queL.push(root.left);
        queR.push(root.right);
       
        while(queL.length !== 0 && queR.length !== 0) {
            let l = queL.shift();
            let r = queR.shift();
            if(l === null && r === null) continue;
            else if(l === null ||  r === null) return false;
            else if( l.val != r.val) {
                return false;
            }
            queL.push(l.left);
            queL.push(l.right);
            queR.push(r.right);
            queR.push(r.left);
        }
        return (queL.length === 0 && queR.length === 0)
    };
    

Log in to reply
 

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