Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2836002

Browse files
authored
Create Invert_Binary_Tree.py
1 parent ae6abaa commit 2836002

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Given the root of a binary tree, invert the tree, and return its root.
2+
3+
class Solution:
4+
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
5+
def solve(root):
6+
if root is None:
7+
return
8+
# Swapping the left and right nodes
9+
root.left, root.right = root.right, root.left
10+
11+
# Recur on both sides
12+
solve(root.left)
13+
solve(root.right)
14+
15+
solve(root)
16+
return root

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /