Monday, September 8, 2014

Leetcode: Binary Tree Level Order Traversal II @Python

# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    # @param root, a tree node
    # @return a list of lists of integers
    def levelOrderBottom(self, root):
        crt=[root]
        rst=[]
        if not crt:
            return []
        while crt!=[None]*len(crt):
            rst.insert(0,[i.val for i in crt])
            nextlvl=[]
            for i in crt:
                if i.left:
                    nextlvl.append(i.left)
                if i.right:
                    nextlvl.append(i.right)
            crt=nextlvl
        return rst      

No comments :

Post a Comment