FastPrepBinary Tree Right Side View

Binary Tree Right Side View

Microsoft logoMicrosoft● MediumFULLTIMEONSITE INTERVIEW
Learn

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 0 through 100000 nodes.
  • Every node value fits in a signed 32-bit integer.

More Microsoft problems

See Microsoft hiring insights
public List<Integer> rightSideView(TreeNode root) {
    // Write your code here.
}
root[1,2,3,null,5,null,4]
expected[1,3,4]
Checking account…