-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path0226-invert-binary-tree.py
34 lines (25 loc) · 1.12 KB
/
0226-invert-binary-tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
Problem: LeetCode 226 - Invert Binary Tree
Key Idea:
To invert a binary tree, we can use a recursive approach. For each node, we swap its left and right subtrees, and then recursively invert the left and right subtrees.
Time Complexity:
The time complexity of this solution is O(n), where n is the number of nodes in the binary tree. We visit each node once to perform the inversion.
Space Complexity:
The space complexity is O(h), where h is the height of the binary tree. In the worst case, the recursion stack can go as deep as the height of the tree.
"""
# 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: TreeNode) -> TreeNode:
if not root:
return None
# Swap left and right subtrees
root.left, root.right = root.right, root.left
# Recursively invert left and right subtrees
self.invertTree(root.left)
self.invertTree(root.right)
return root