forked from chenganqi498/leecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LevelOrder.py
54 lines (43 loc) · 1.37 KB
/
LevelOrder.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
# Binary tree level order
class Treenode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def levelOrder(self, tree):
if tree is None:
return []
tol = [[tree.val]]
parents = [tree]
while parents != []:
current = self.CurrentLevel(parents)
if current == []:
break
level = []
for i in current:
level.append(i.val)
tol.append(level)
parents = current
return tol
def CurrentLevel(self, parents):
current = []
for node in parents:
if node.left != None:
current.append(node.left)
if node.right != None:
current.append(node.right)
return current
if __name__ == '__main__':
tree = Treenode(3)
tree.left = Treenode(9)
tree.right = Treenode(0)
tree.right.left = Treenode(15)
tree.right.right = Treenode(7)
tree.left.right = Treenode(20)
tree.left.right.right = Treenode(25)
tree.left.right.left = Treenode(30)
tree.left.right.left.right = Treenode(13)
out = Solution()
#print [tree], level
print out.levelOrder(tree)