Leetcode 102 Binary Tree Level Order Traversal

Posted Fourth Dimension

tags:

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

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

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

    3
   /   9  20
    /     15   7

 

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
] 
class Solution(object):
    def levelOrder(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.append(temp)
            stack = stack_new
        return ans
                        

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

Leetcode 102. Binary Tree Level Order Traversal

[LeetCode]题解(python):102- Binary Tree Level Order Traversal

LeetCode 102. Binary Tree Level Order Traversal

Leetcode 102. Binary Tree Level Order Traversal

[leetcode-102-Binary Tree Level Order Traversal]

Java [Leetcode 102]Binary Tree Level Order Traversal