FastPrepFlatten Binary Tree to Linked List

Flatten Binary Tree to Linked List

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Flatten the binary tree rooted at root in place into a right-child-only chain whose node order is the tree's preorder traversal. Set every left child to null and return root.

Function

flattenTree(root: TreeNode) → TreeNode

Examples

Example 1

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

The right chain follows preorder 1,2,3,4,5,6.

Constraints

  • The tree contains at most 2000 nodes.
  • Node values fit in signed 32-bit integers.

More Bloomberg LP problems

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