题解 | #求二叉树的层序遍历#Python 无引用queue包
求二叉树的层序遍历
http://www.nowcoder.com/practice/04a5560e43e24e9db4595865dc9c63a3
class Solution:
def levelOrder(self , root ):
# write code here
p = root
if not p:
return None
list = []
list1 = []
list1.append(p)
while list1:
list2 = [] #存放指针
list3 = [] #存放val值
for i in range(len(list1)):
list3.append(list1[i].val)
if list1[i].left:
list2.append(list1[i].left)
if list1[i].right:
list2.append(list1[i].right)
list.append(list3)
list1 = list2
return list
查看13道真题和解析