FastPrepBoundary of Binary Tree

Boundary of Binary Tree

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Given a binary tree, return its anti-clockwise boundary: the root, the left boundary excluding leaves, every leaf from left to right, and the right boundary excluding leaves in bottom-up order.

Do not include any node more than once.

Function

boundaryOfBinaryTree(root: TreeNode) → int[]

Examples

Example 1

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

Three and four are leaves, and two closes the right boundary.

Example 2

root = [1,2,3,4,5,6,7]return = [1,2,4,5,6,7,3]

The boundary walks around the complete tree without duplicates.

Constraints

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

More Bloomberg LP problems

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