Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Binary Tree Right Side View #62

Open
kokocan12 opened this issue Jul 17, 2022 · 0 comments
Open

Binary Tree Right Side View #62

kokocan12 opened this issue Jul 17, 2022 · 0 comments
Assignees

Comments

@kokocan12
Copy link
Owner

Problem

When a tree is given and you are standing right side of tree.
Return values from top to bottom.

Approach

Using DFS with level, value of level is overwritten by right most value.

const rightSideView = function(root) {
    const res = [];
    
    function walk(node, level=0) {
        if(!node) return;
        
        res[level] = node.val;
        
        walk(node.left, level+1);
        walk(node.right, level+1);
    }
    
    walk(root);
    
    return res;
};
@kokocan12 kokocan12 self-assigned this Jul 17, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

1 participant