Easy javascript solution without recursion


  • 0
    B
    var rightSideView = function(root) {
        var rightViews = [];
        
        if (root) {
            var stack = [root];
            
            do { 
                var node = null;
                var nextLevel = [];
                while(stack.length > 0) {
                    node = stack.shift();
                    
                    if (node.left)
                        nextLevel.push(node.left);
                    
                    if (node.right)
                        nextLevel.push(node.right);
                }
                
                rightViews.push(node.val);
                stack = nextLevel;
                
            } while(stack.length > 0)
        }
        
        return rightViews;
    };

Log in to reply
 

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