LintCode:二叉树的层次遍历 II

LintCode:二叉树的层次遍历 II

""" Definition of TreeNode: class TreeNode: def __init__(self, val): self.val = val self.left, self.right = None, None """


class Solution:
    """ @param root: The root of binary tree. @return: buttom-up level order in a list of lists of integers """
    def levelOrderBottom(self, root):
        # write your code here
        L = []
        if root == None:
            return []
        L.append([root.val])
        L.append([])
        if root.left != None:
            L[1].append(root.left)
        if root.right != None:
            L[1].append(root.right)
        m = 2

        while len(L[1]) != 0:
            n = len(L[1])
            L.append([])
            for i in range(n):
                if L[1][0].left != None:
                    L[1].append(L[1][0].left)
                if L[1][0].right != None:
                    L[1].append(L[1][0].right)
                L[m].append(L[1][0].val)
                L[1].remove(L[1][0])
            m += 1
        L.remove(L[1])
        return L[::-1]

你可能感兴趣的:(LintCode:二叉树的层次遍历 II)