Simple Recursive JS


  • 0
    P
    var rightSideView = function(root) {
        if (!root) return [];
        var items = [root.val];
        helper(root, 0, 0, items)
        return items;
    };
    
    var helper = function(root, depth, minDepth, items) {
        if (root == null) return Math.max(depth - 1, minDepth)
        if (depth > minDepth) items.push(root.val);
        var rightMaxDepth = helper(root.right, depth + 1, minDepth, items);
        var leftMaxDepth = helper(root.left, depth + 1, rightMaxDepth, items);
        return Math.max(rightMaxDepth, leftMaxDepth)
    
    
    }
    

Log in to reply
 

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