Skip to content

Commit 8188ff5

Browse files
committed
add trees
1 parent 266b61a commit 8188ff5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/trees/invert_binary_tree.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import Optional
2+
3+
# Definition for a binary tree node.
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
11+
class Solution:
12+
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
13+
if not root:
14+
return None
15+
16+
tmp = root.left
17+
root.left = root.right
18+
root.right = tmp
19+
20+
self.invertTree(root.left)
21+
self.invertTree(root.right)
22+
return root

0 commit comments

Comments
 (0)