Invert Binary Tree

Read the lesson → understand the pattern → then code. Tutor works offline (built-in teacher); optional Cursor/Gemini keys upgrade chat quality.

Topic refresher: Binary Trees — Recursion Native

Answer for a node from left + right children.

int maxDepth(TreeNode* root) {
    if (!root) return 0;
    return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
Binary Trees

Invert Binary Tree

Easy
LeetCode
1/3
EasyBinary TreesLeetCode #226

Invert Binary Tree

Swap left/right recursively (or BFS).

Examples

Example 1

Input

4 2 7 1 3 6 9

Output

4 7 2 9 6 3 1

Stuck? Take a nudge

C++17