226. Invert Binary Tree
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to invert a binary tree. This means that for every node in the tree, its left child and its right child must be swapped. This operation needs to be applied recursively to the entire tree, effectively creating a mirror image of the original tree structure. The function should then return the root of this newly inverted tree.
Solution
Invert Binary Tree in Python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
# Base case: if the root is None, return None.
if not root:
return None
# Recursively invert the left and right subtrees.
# This is a post-order traversal approach: process children first.
self.invertTree(root.left)
self.invertTree(root.right)
# Swap the left and right children.
root.left, root.right = root.right, root.left
return root
Invert Binary Tree in Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
// Base case: if the node is null, return null.
if (root == null) {
return null;
}
// Recursively invert the left subtree.
invertTree(root.left);
// Recursively invert the right subtree.
invertTree(root.right);
// Swap the left and right children of the current node.
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
// Return the root of the now inverted tree.
return root;
}
}
Invert Binary Tree in C++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
// Base case: if the tree is empty, there's nothing to invert.
if (root == nullptr) {
return nullptr;
}
// Recursively invert the left subtree.
invertTree(root->left);
// Recursively invert the right subtree.
invertTree(root->right);
// Swap the left and right children of the current node.
TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
// Return the root of the inverted tree.
return root;
}
};
Invert Binary Tree in TypeScript
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function invertTree(root: TreeNode | null): TreeNode | null {
// Base case: if the root is null, there's nothing to do.
if (root === null) {
return null;
}
// Recursively invert the left and right subtrees.
invertTree(root.left);
invertTree(root.right);
// Swap the left and right children of the current node.
const temp = root.left;
root.left = root.right;
root.right = temp;
return root;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.