美文网首页Leetcode
107. Binary Tree Level Order Tra

107. Binary Tree Level Order Tra

作者: oo上海 | 来源:发表于2016-08-05 04:01 被阅读16次

    107. Binary Tree Level Order Traversal II

    题目:

    https://leetcode.com/problems/binary-tree-level-order-traversal-ii/

    难度:

    Easy

    用102 的算法作弊

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def levelOrderBottom(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            res = []
            
            if root == None: return []
            
            curLevel = [root]
            while curLevel:
                nextLevel = []
                tmpRes = []
                for node in curLevel:
                    tmpRes.append(node.val)
                    if node.left: nextLevel.append(node.left)
                    if node.right: nextLevel.append(node.right)
                res.append(tmpRes)
                curLevel = nextLevel
            res.reverse()
            return res
    

    相关文章

      网友评论

        本文标题:107. Binary Tree Level Order Tra

        本文链接:https://www.haomeiwen.com/subject/nkdhsttx.html