easyBinary TreeRecursionTrees 0 views
Invert Binary Tree
Flip a binary tree into its mirror image by swapping every node's left and right children.
Given the root of a binary tree, invert the tree (i.e., mirror it), and return the level-order representation of the resulting tree.
Inverting a tree means that for every node, its left and right children are swapped -- recursively, all the way down.
Example 1
Input: root = [4,2,7,1,3,6,9]
Output: [4,7,2,9,6,3,1]
Example 2
Input: root = [2,1,3]
Output: [2,3,1]
Example 3
Input: root = []
Output: []
Explanation: An empty tree inverts to itself.
Constraints
- The number of nodes in the tree is in the range [0, 100].
- -100 <= Node.val <= 100
Follow-up
Can you solve it both recursively and iteratively (using a queue or stack)?
Hints
Companies
No companies reported yet.
Discussion
Sign in to join the discussion.
Loading discussion...
Test results
No test cases yet.