Skip to content
BytePatterns

Right Edge View

MediumTrees & BST#bfs#level-order~25m

Problem

Imagine standing to the right of a binary tree and looking straight at it. Return the values you can see, ordered from the top of the tree downward. Exactly one value is visible per level: the rightmost node on that level, whichever subtree it happens to belong to.

Examples

Input:  tree = 1, left child 2 with right child 5, right child 3 with right child 4
Output: [1, 3, 4]
Why:    node 5 is hidden behind node 4 on the same level
Input:  tree = 1 with a single left child 2
Output: [1, 2]
Why:    edge case, a left-only node is still the rightmost on its level
Input:  tree = empty
Output: []
Why:    edge case, there is nothing to see

Hints

0 / 3

Stuck on the idea rather than the code? Breadth-First Search covers it.