Skip to content

107 binary tree level order traversal ii

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


回到顶部