class Solution:
def listOfDepth(self, tree: TreeNode) -> List[ListNode]:
def fun(x,y):
x.next = y
return x.next #要return,后面能用reduce,希望什么作为x传入下一次迭代呢?当然是当前链表的尾结点,即 y
def getList(a): #传入一个 list a = [1,2,3]
nodes = list(map(lambda x:ListNode(x),a)) #[1,2,3]转为ListNode类型
tail = reduce(fun,nodes) # lambda 好像并不能类似赋值,只能自定义函数了
tail.next = None
return nodes[0]
# 主函数域
res = []
q = []
q.insert(0,tree)
while q:
size = len(q)
a = []
while size:
out = q.pop()
a.append(out.val)
if out.left:
q.insert(0,out.left)
if out.right:
q.insert(0,out.right)
size -= 1
cur_list = getList(a)
res.append(cur_list)
return res