FastPrepInvert Binary Tree

Invert Binary Tree

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Given the root of a binary tree, invert the tree by swapping the left and right child of every node. Return the root of the inverted tree.

Function

invertTree(root: TreeNode) → TreeNode

Examples

Example 1

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

Every node's two child links are swapped.

Example 2

root = []return = []

An empty tree remains empty.

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 TreeNode invertTree(TreeNode root) {
    // Write your code here.
}
root[4,2,7,1,3,6,9]
expected[4,7,2,9,6,3,1]
Checking account…