forked from DmrfCoder/AlgorithmAndDataStructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path65.py
56 lines (49 loc) · 1.5 KB
/
65.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
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回二维列表[[1,2],[4,5]]
def Print(self, pRoot):
# write code here
# write code here
if pRoot is None:
return []
a = []
b = []
flag = 0
res = []
a.append(pRoot)
res.append([pRoot.val])
while len(a) != 0 or len(b) != 0:
if flag == 0:
curNode = a[0]
del a[0]
if curNode.left is not None:
b.append(curNode.left)
if curNode.right is not None:
b.append(curNode.right)
if len(a) == 0:
flag = 1
temp = []
for itemB in b:
temp.append(itemB.val)
if len(temp) != 0:
res.append(temp)
else:
curNode = b[0]
del b[0]
if curNode.left is not None:
a.append(curNode.left)
if curNode.right is not None:
a.append(curNode.right)
if len(b) == 0:
flag = 0
temp = []
for itemA in a:
temp.append(itemA.val)
if len(temp) != 0:
res.append(temp)
return res