Skip to content

Commit f7f6355

Browse files
author
Partho Biswas
committed
no message
1 parent bd49e52 commit f7f6355

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
788788
|03| [Iterative_In-order_Traversal](algoexpert.io/questions/Iterative_In-order_Traversal.md) | [Python](algoexpert.io/python/Iterative_In-order_Traversal.py) |
789789
|04| [Right_Sibling_Tree](algoexpert.io/questions/Right_Sibling_Tree.md) | [Python](algoexpert.io/python/Right_Sibling_Tree.py) |
790790
|05| [Branch_Sums](algoexpert.io/questions/Branch_Sums.md) | [Python](algoexpert.io/python/Branch_Sums.py) |
791+
|06| [Flatten_Binary_Tree](algoexpert.io/questions/Flatten_Binary_Tree.md) | [Python](algoexpert.io/python/Flatten_Binary_Tree.py) |
791792

792793
</p>
793794
</details>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# This is the class of the input root. Do not edit it.
2+
class BinaryTree:
3+
def __init__(self, value, left=None, right=None):
4+
self.value = value
5+
self.left = left
6+
self.right = right
7+
8+
def flattenBinaryTree(root):
9+
iorderNodes = getNodesInOrder(root, [])
10+
for i in range(0, len(iorderNodes) - 1):
11+
leftNode = iorderNodes[i]
12+
rightNode = iorderNodes[i + 1]
13+
leftNode.right = rightNode
14+
rightNode.left = leftNode
15+
return iorderNodes[0]
16+
17+
18+
def getNodesInOrder(tree, array):
19+
if tree is not None:
20+
getNodesInOrder(tree.left, array)
21+
array.append(tree)
22+
getNodesInOrder(tree.right, array)
23+
return array
24+

algoexpert.io/python/Same_BSTs.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def sameBsts(arrayOne, arrayTwo):
2+
if len(arrayOne) != len(arrayTwo):
3+
return False
4+
5+
if len(arrayOne) == 0 and len(arrayTwo) == 0:
6+
return True
7+
8+
if arrayOne[0] != arrayTwo[0]:
9+
return False
10+
11+
leftOne = getSmaller(arrayOne)
12+
leftTwo = getSmaller(arrayTwo)
13+
rightOne = getBiggerOrEqual(arrayOne)
14+
rightTwo = getBiggerOrEqual(arrayTwo)
15+
16+
return sameBsts(leftOne, leftTwo) and sameBsts(rightOne, rightTwo)
17+
18+
19+
def getSmaller(array):
20+
smaller = []
21+
for i in range(1, len(array)):
22+
if array[i] < array[0]:
23+
smaller.append(array[i])
24+
return smaller
25+
26+
27+
def getBiggerOrEqual(array):
28+
biggerOrEqual = []
29+
for i in range(1, len(array)):
30+
if array[i] >= array[0]:
31+
biggerOrEqual.append(array[i])
32+
return biggerOrEqual
33+

0 commit comments

Comments
 (0)