Binary Tree Right Side View
Problem statement
Given the root of a binary tree, imagine standing to the right of it. Return the value of the node visible at each depth, ordered from the root level downward.
When several nodes share a depth, the rightmost node is visible.
Function
rightSideView(root: TreeNode) → List<Integer>Examples
Example 1
root = [1,2,3,null,5,null,4]return = [1,3,4]The rightmost values at depths zero, one, and two are 1, 3, and 4.
Example 2
root = [1,null,3]return = [1,3]The tree has one visible node at each of its two depths.
Example 3
root = []return = []An empty tree has no visible nodes.
Constraints
- The tree contains from
0through100000nodes. - Every node value fits in a signed 32-bit integer.