Return right and left side views of a tree
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Given the root of a binary tree, compute two “side views”:
1. `right_view`: the list of node values visible when looking at the tree from the **right** side, ordered from **top to bottom**.
2. `left_view_reversed`: the list of node values visible when looking at the tree from the **left** side, but ordered from **bottom to top** (i.e., the left side view, then reversed).
Return both lists.
## Definitions
- The side view at a given depth is the value of the node you would see from that side (i.e., the last node encountered at that depth when scanning from that side).
## Input
- `root`: root node of a binary tree.
## Output
- A pair/tuple: `(right_view, left_view_reversed)`.
## Constraints
- Number of nodes: `0..2*10^5`
- Node values fit in 32-bit signed integer.
## Example
Tree:
```
1
/ \
2 3
\ \
5 4
```
- `right_view = [1, 3, 4]`
- `left_view = [1, 2, 5]` so `left_view_reversed = [5, 2, 1]`
Quick Answer: This question evaluates understanding of binary tree data structures and the competency to identify side views by reasoning about which node is visible at each depth.
Given level-order tree values, return [right_view, left_view_reversed].
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: ([1,2,3,None,5,None,4],)
Expected Output: [[1, 3, 4], [5, 2, 1]]
Explanation: Prompt example shape.
Input: ([],)
Expected Output: [[], []]
Explanation: Empty tree.
Input: ([1,2,None,3],)
Expected Output: [[1, 2, 3], [3, 2, 1]]
Explanation: Left skew.
Hints
- Pick a representation that makes the requested operation direct.
- Handle empty inputs and boundary cases first.