leetcode--二叉树的层次遍历-创新互联

给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。

站在用户的角度思考问题,与客户深入沟通,找到城口网站设计与城口网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站制作、成都做网站、企业官网、英文网站、手机端网站、网站推广、域名注册雅安服务器托管、企业邮箱。业务覆盖城口地区。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]
# Definition for a binary tree node. # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = None #         self.right = None class Solution:     def levelOrder(self, root: TreeNode) -> List[List[int]]:         if not root:             return []         res = []         cur_node = [root]         next_node = []         res.append([i.val for i in cur_node])         while cur_node or next_node:             for node in cur_node:                 if node.left:                     next_node.append(node.left)                 if node.right:                     next_node.append(node.right)             if next_node:                 res.append([                     i.val for i in next_node                 ])             cur_node = next_node             next_node = []         return res

执行用时 : 80 ms, 在Binary Tree Level Order Traversal的Python3提交中击败了26.32% 的用户

内存消耗 : 13.2 MB, 在Binary Tree Level Order Traversal的Python3提交中击败了98.08% 的用户

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章题目:leetcode--二叉树的层次遍历-创新互联
本文网址:http://pwwzsj.com/article/dphsjj.html