Skip to main content

🟡 剑指 Offer 32 - I. 从上到下打印二叉树

LeetCode 提示

题目难度 中等

原题链接 🔗 leetcode

题解 1.py#

# 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[int]:        if root is None:            return []
        levl = [root]        res = []        while levl:            len1 = len(levl)            for idx in range(len1):                n = levl[idx]                res.append(n.val)                if n.left:                    levl.append(n.left)                if n.right:                    levl.append(n.right)            levl = levl[len1:]        return res

题解 2_更快.py#

# 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[int]:        if root is None:            return []
        levl = [root]        res = []        while levl:            n = levl.pop(0)            res.append(n.val)            if n.left:                levl.append(n.left)            if n.right:                levl.append(n.right)        return res