Leetcode 107 Binary Tree Level Order Traversal II

Posted Fourth Dimension

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 107 Binary Tree Level Order Traversal II相关的知识,希望对你有一定的参考价值。

Given a binary tree, return the bottom-up level order traversal of its nodes‘ values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
   /   9  20
    /     15   7

 

return its bottom-up level order traversal as:

[
  [15,7],
  [9,20],
  [3]
]
class Solution(object):
    def levelOrderBottom(self, root):
        if not root:
            return []
        stack, ans = [root], []
        while stack:
            temp, stack_new = [], []
            for x in stack:
                temp.append(x.val)
                if x.left:
                    stack_new.append(x.left)
                if x.right:
                    stack_new.append(x.right)
            ans = [temp] + ans
            stack = stack_new
        return ans
 

以上是关于Leetcode 107 Binary Tree Level Order Traversal II的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode107. Binary Tree Level Order Traversal II 解题报告

LeetCode_107. Binary Tree Level Order Traversal II

Leetcode 107 Binary Tree Level Order Traversal II

LeetCode107 Binary Tree Level Order Traversal II

Leetcode 107. Binary Tree Level Order Traversal II

LeetCode 107.Binary Tree Level Order Traversal II