-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevelOrderTraversal2.py
executable file
·59 lines (49 loc) · 1.94 KB
/
LevelOrderTraversal2.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from typing import List, Optional
from BinaryTree import TreeNode, BinaryTree
class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
if root == None:
return []
if root.left == None and root.right == None:
return [[root.val]]
returnList = self.find_bottom_and_return([root])
returnList.append([root.val])
return returnList
def find_bottom_and_return(self, queue: List[TreeNode]) -> List[List]:
if(len(queue) == 0):
return []
newQueue = []
for i in range(0, len(queue)):
if queue[i].left != None:
newQueue.append(queue[i].left)
if queue[i].right != None:
newQueue.append(queue[i].right)
bottom = self.find_bottom_and_return(newQueue)
if len(newQueue) == 0:
return bottom
newQueue = list(map(lambda element : element.val, newQueue))
bottom.append(newQueue)
return bottom
def levelOrderUsingLoop(self, root: Optional[TreeNode]) -> List[List[int]]:
if root == None:
return []
queue = [root]
levels = [[root.val]]
while(len(queue) != 0):
currentList = []
for i in range(0,len(queue)):
element = queue.pop(0)
if element.left:
currentList.append(element.left.val)
queue.append(element.left)
if element.right:
currentList.append(element.right.val)
queue.append(element.right)
if len(currentList) != 0:
levels.insert(0, currentList)
return levels
bt = BinaryTree()
root = bt.create_tree_from_array([3,9,20,None,None,15,7])
reverse_level_order = Solution().levelOrderBottom(root)
print(reverse_level_order)
print(Solution().levelOrderUsingLoop(root))