-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.py
32 lines (30 loc) · 874 Bytes
/
Solution.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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def listOfDepth(self, tree: TreeNode) -> List[ListNode]:
q = [tree]
ans = []
while q:
n = len(q)
head = ListNode(-1)
tail = head
for i in range(n):
front = q.pop(0)
node = ListNode(front.val)
tail.next = node
tail = node
if front.left:
q.append(front.left)
if front.right:
q.append(front.right)
ans.append(head.next)
return ans