Invert Binary Tree
Swap the left and right children at every node of a binary tree and return its root. Trees use breadth-first value arrays with null for missing children.
Example 1
root = [4,2,7,1,3][4,7,2,null,null,3,1]Explanation
Swap 2 and 7, then 1 and 3 under node 2; null holes preserve the new shape.
Example 2
root = []nullExplanation
Inverting an empty tree returns an empty tree.
Example 3
root = [8][8]Explanation
A leaf has no children to swap.
Constraints
- 0 <= number of nodes <= 10000
- Node values are signed 32-bit integers
Function contract
invertTreeArguments: treeSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 34 cases, on your device.
[4,2,7,1,3]