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,null,null,15,7]
,
3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]
# 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 levelOrder(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ s = [] nodes = [] if not root: return s nodes.append(root) while nodes: s.append([n.val for n in nodes]) tmp = [] for node in nodes: if node.left: tmp.append(node.left) if node.right: tmp.append(node.right) nodes = tmp return s
相关推荐
js js_leetcode题解之107-binary-tree-level-order-traversal-ii.js
Construct Binary Tree from Preorder and Inorder Traversal 根据先序,中序建立二叉树
java java_leetcode-107-binary-tree-level-order-traversal
java java_leetcode-102-binary-tree-level-order-traversal
python python_leetcode题解之107_Binary_Tree_Level_Order_Traversal_II
在"binary tree traversal binary tree.doc"文档中,可能会详细探讨这些概念并给出具体的代码示例,包括如何实现这些遍历方法以及在不同场景下如何选择合适的二叉树结构。通过学习和理解这些内容,开发者能够更好地...
js js_leetcode题解之102-binary-tree-level-order-traversal.js
js js_leetcode题解之103-binary-tree-zigzag-level-order-traversal.js
python python_leetcode题解之102_Binary_Tree_Level_Order_Traversal
python python_leetcode题解之103_Binary_Tree_Zigzag_Level_Order_Traversal
我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的...102. Binary Tree Leve
94.Binary_Tree_Inorder_Traversal二叉树的中序遍历【LeetCode单题讲解系列】
这个压缩包文件"Pre-order-binary-tree-traversal.rar_pre_pre-orderC语言"显然是为了教授如何使用C语言进行二叉树的前序遍历。下面将详细介绍前序遍历的概念、算法以及C语言实现的关键点。 1. **前序遍历的概念**...
javascript js_leetcode题解之94-binary-tree-inorder-traversal.js
java java_leetcode-105-construct-binary-tree-from-preorder-and-inorde
java java_leetcode-114-flatten-binary-tree-to-linked-list
python python_leetcode题解之094_Binary_Tree_Inorder_Traversal
javascript js_leetcode题解之124-binary-tree-maximum-path-sum.js
javascript js_leetcode题解之114-flatten-binary-tree-to-linked-list.js
c语言基础 c语言_leetcode题解之0094_binary_tree_inorder_traversal.zip