码迷,mamicode.com
首页 > 编程语言 > 详细

LeetCode | 0102. Binary Tree Level Order Traversal二叉树的层次遍历【Python】

时间:2020-03-20 23:49:28      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:list   维数   int   etc   ==   ref   问题   循环   help   

LeetCode 0102. Binary Tree Level Order Traversal二叉树的层次遍历【Medium】【Python】【BFS】

Problem

LeetCode

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]
]

问题

力扣

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

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

    3
   /   9  20
    /     15   7

返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]

思路

BFS

解法一:非递归
当队列不为空:
    当前层打印循环:
        队首元素出队,记为 node
        将 node.val 添加到 temp 尾部
        若左(右)子节点不为空,则将左(右)子节点加入队列
    把当前 temp 中的所有元素加入 res

时间复杂度: O(n),n 为二叉树的节点数。
空间复杂度: O(n),n 为二叉树的节点数。

Python3代码
# 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]]:
        # solution one: 非递归
        import collections
        if not root:
            return []
        
        res, q = [], collections.deque()
        q.append(root)
        while q:
            # 输出是二维数组
            temp = []
            for x in range(len(q)):
                node = q.popleft()
                temp.append(node.val)
                if node.left:
                    q.append(node.left)
                if node.right:
                    q.append(node.right)
            res.append(temp)
        return res
解法二:递归
Python3代码
# 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]]:
        # solution two: 递归
        res = []
        
        def helper(root, depth):
            if not root:
                return
            if len(res) == depth:
                res.append([])
            res[depth].append(root.val)
            helper(root.left, depth + 1)
            helper(root.right, depth + 1)
        helper(root , 0)
        return res

GitHub链接

Python

LeetCode | 0102. Binary Tree Level Order Traversal二叉树的层次遍历【Python】

标签:list   维数   int   etc   ==   ref   问题   循环   help   

原文地址:https://www.cnblogs.com/wonz/p/12535716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!