Why my javascript code doesn't work?


  • 0
    A
    function arraysEqual(arr1, arr2) {
        if(arr1.length !== arr2.length)
            return false;
        for(var i = arr1.length; i--;) {
            if(arr1[i] !== arr2[i])
                return false;
        }
    
        return true;
    }
     
    var inorder = function(resultArray, root, leftOrRight){
        if(root === null){
            resultArray.push('null');
            return;
        }
        resultArray.push(root.val);
        if(root.left !== null && root.right !== null){
            if(leftOrRight === 'L'){
                inorder(resultArray, root.left, 'L');
                inorder(resultArray, root.right, 'R');
            }
            else{
                inorder(resultArray, root.right, 'R');
                inorder(resultArray, root.left, 'L');
            }
        }
        return;
    }
    
    var isSymmetric = function(root) {
        if(root === null){
            return true;
        }
        var resultR = [], resultL = [];
        inorder(resultR, root.right, 'R');
        inorder(resultL, root.left, 'L');
        
        if(arraysEqual(resultL, resultR)){
            return true;
        }
        else{
            return false;
        }
    };

Log in to reply
 

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