FastPrepBinary Tree Right View

Binary Tree Right View

Pinecone logoPinecone● MediumFULLTIMEOA
Learn

Problem statement

Given the root of a binary tree, imagine viewing the tree from its right side.

Return the value of the visible node at each depth, ordered from the root level downward. Return an empty array for an empty tree.

Function

rightSideView(root: TreeNode) → int[]

Examples

Example 1

root = [1,2,3,null,5,null,4]return = [1,3,4]

The rightmost values at successive depths are 1, 3, and 4.

Example 2

root = [1,null,3]return = [1,3]

The right child is visible below the root.

Example 3

root = []return = []

An empty tree has no visible levels.

Constraints

  • The tree contains between 0 and 10000 nodes.
  • -10^9 <= Node.val <= 10^9.

More Pinecone problems

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