Skip to main content

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

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[List[int]]:        if root is None:            return []        res = []        levl = [root]        flag = True        while levl:            subRes = []            len1 = len(levl)            for _ in range(len1):                n = levl.pop(0)                if flag:                    subRes.append(n.val)                else:                    subRes.insert(0, n.val)                if n.left:                    levl.append(n.left)                if n.right:                    levl.append(n.right)            res.append(subRes)            flag = not flag        return res