Skip to content

Commit ccfa4b7

Browse files
committed
Adding Efficient Solution for Problem - 226 - Invert BT
1 parent 3e033bd commit ccfa4b7

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Invert_BT/EfficientSolution.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 226
6+
## Problem Name: Invert Binary Tree
7+
##===================================
8+
#Invert a binary tree.
9+
#
10+
#Example:
11+
#
12+
#Input:
13+
#
14+
# 4
15+
# / \
16+
# 2 7
17+
# / \ / \
18+
#1 3 6 9
19+
#Output:
20+
#
21+
# 4
22+
# / \
23+
# 7 2
24+
# / \ / \
25+
#9 6 3 1
26+
class Solution:
27+
def invertTree(self, root):
28+
if root is None: #Condition-check:If root is empty
29+
return root #We return root
30+
else: #Condition-check: Else
31+
left = self.invertTree(root.right) #Use recursion to invert left values
32+
right = self.invertTree(root.left) #Use recursion to invert right values
33+
root.left = left #Update root.left tree to left
34+
root.right = right #Update root.right tree to right
35+
return root #We return root

0 commit comments

Comments
 (0)