1. What I learned
Insert two different elements into a list
To reverse the order of elements in a list, write as follows.
LIST = [1,2,3,4,5]
print(list(reversed(LIST)))
#I will print [5,4,3,2,1]
2. How I sloved
Values of the same height should be in the same element of the list. I used the BFS and added the height of the value when I put the value in the queue. When pop-up a queue, I checked the height first. If the value is the first at that height, I put it in “visited” list. Otherwise, this value was added to the same height element in the list.
3. Code
class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
if root == None:
return []
visited = []
queue = deque([(root,0)])
while queue:
node,height = queue.popleft()
if height>=len(visited):
visited.append([node.val])
else:
visited[height].append(node.val)
if node.left:
queue.append((node.left,height+1))
if node.right:
queue.append((node.right,height+1))
return list(reversed(visited))
4. Result
Runtime : 32 ms(83.93%), Memory usage : 14.2 MB(16.09%)
(Runtime can be different by a system even if it is a same code.)