DSA · Trees · #89
Invert Binary Tree
Module 44 · difficulty 2/5·⏱ 30:00starts on first keystroke
Given the `root` of a binary tree, invert the tree (swap every node's left and right child, recursively) and return its `root`. Implement the function `invertTree(root)`, where each tree node is `{ val, left, right }` and an empty tree is represented by `null`.
Examples
root = [4,2,7,1,3,6,9]→[4,7,2,9,6,3,1]— Every left/right pair is swapped at all levels.root = [2,1,3]→[2,3,1]root = []→[]— An empty tree inverts to an empty tree.
Constraints
- · The number of nodes in the tree is in the range [0, 100].
- · -100 <= Node.val <= 100
- · The tree may be skewed (effectively a linked list).
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.