Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Invert Binary Tree in Python
Inverting a binary tree means swapping the left and right child nodes for every node in the tree. This creates a mirror image of the original tree structure.
Recursive Approach
The algorithm follows these steps ?
- If the root is null, return immediately
- Swap the left and right child pointers of the current node
- Recursively invert the left subtree
- Recursively invert the right subtree
Implementation
Tree Node Definition
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
Invert Tree Function
def invert_tree(root):
# Base case: if node is None, return None
if not root:
return None
# Swap left and right children
root.left, root.right = root.right, root.left
# Recursively invert left and right subtrees
invert_tree(root.left)
invert_tree(root.right)
return root
Helper Functions for Display
def print_inorder(root):
"""Print tree in inorder traversal"""
if root:
print_inorder(root.left)
print(root.val, end=' ')
print_inorder(root.right)
def print_level_order(root):
"""Print tree level by level"""
if not root:
return
queue = [root]
while queue:
node = queue.pop(0)
if node:
print(node.val, end=' ')
queue.append(node.left)
queue.append(node.right)
else:
print('None', end=' ')
Complete Example
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def invert_tree(root):
if not root:
return None
# Swap left and right children
root.left, root.right = root.right, root.left
# Recursively invert subtrees
invert_tree(root.left)
invert_tree(root.right)
return root
def print_inorder(root):
if root:
print_inorder(root.left)
print(root.val, end=' ')
print_inorder(root.right)
# Create example tree: [4,2,7,1,3,6,9]
root = TreeNode(4)
root.left = TreeNode(2)
root.right = TreeNode(7)
root.left.left = TreeNode(1)
root.left.right = TreeNode(3)
root.right.left = TreeNode(6)
root.right.right = TreeNode(9)
print("Original tree (inorder):")
print_inorder(root)
print()
# Invert the tree
inverted_root = invert_tree(root)
print("Inverted tree (inorder):")
print_inorder(inverted_root)
print()
Original tree (inorder): 1 2 3 4 6 7 9 Inverted tree (inorder): 9 7 6 4 3 2 1
Iterative Approach
You can also solve this problem iteratively using a queue ?
def invert_tree_iterative(root):
if not root:
return None
queue = [root]
while queue:
current = queue.pop(0)
# Swap left and right children
current.left, current.right = current.right, current.left
# Add children to queue for processing
if current.left:
queue.append(current.left)
if current.right:
queue.append(current.right)
return root
# Test with same tree structure
root = TreeNode(4)
root.left = TreeNode(2)
root.right = TreeNode(7)
root.left.left = TreeNode(1)
root.left.right = TreeNode(3)
root.right.left = TreeNode(6)
root.right.right = TreeNode(9)
print("Using iterative approach:")
inverted = invert_tree_iterative(root)
print_inorder(inverted)
Using iterative approach: 9 7 6 4 3 2 1
Comparison
| Approach | Time Complexity | Space Complexity | Implementation |
|---|---|---|---|
| Recursive | O(n) | O(h) where h is height | More intuitive |
| Iterative | O(n) | O(w) where w is max width | Avoids recursion stack |
Conclusion
Inverting a binary tree swaps left and right children at every node. The recursive approach is more intuitive, while the iterative approach using a queue avoids potential stack overflow for deep trees.
---Advertisements
