Binary Tree Level Order Traversal II


  • 0
    L

    from collections import deque
    class Solution(object):
    def levelOrderBottom(self, root):
    queue = deque()
    queue.append(root)
    list = []
    if root is None:
    return list
    list.append([root.val])
    while len(queue)!=0:
    tem_list = []
    for i in range(len(queue)):
    tem = queue.popleft()
    if tem.left is not None:
    queue.append(tem.left)
    tem_list.append(tem.left.val)
    if tem.right is not None:
    queue.append(tem.right)
    tem_list.append(tem.right.val)
    if tem_list:
    list.append(tem_list)
    list.reverse()
    return list


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.