Short Javascript solution beats 100%


  • 0
    M
    var rightSideView = function(root) {
       var next = [{node:root,level:0}];
       var sight = []
       while(next.length != 0){
           var curr = next.pop()
           if(!curr.node) continue;
           if(curr.level == sight.length){
              sight.push(curr.node.val)
           }
           if(curr.node.left){
              next.push({node:curr.node.left,level:curr.level+1})
           }
           if(curr.node.right){
              next.push({node:curr.node.right,level:curr.level+1})
           }
       }
       return sight
    

    };


Log in to reply
 

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