Straight forward recursive solution


  • 0
    P
    var rightSideView = function(root) {
        if(!root) return [];
        var items = [root.val];
        helper(root, 0, items)
        return items;
    };
    
    var helper = function(root, currLevel, items) {
        if(root==null) return
        if(!items[currLevel]) items[currLevel] = root.val;
        helper(root.right, currLevel+1, items)
        helper(root.left, currLevel+1, items)   
    }
    

Log in to reply
 

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