本文主要是介绍Leetcode:剑指Offer 32 - III从上到下打印二叉树IIII,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目
答案:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = Noneclass Solution:def levelOrder(self, root: TreeNode) -> List[List[int]]:if not root:return []res,deque = [],collections.deque([root])while deque:tmp = collections.deque()for _ in range(len(deque)):node = deque.popleft()if len(res)%2:tmp.appendleft(node.val)else:tmp.append(node.val)if node.left:deque.append(node.left)if node.right:deque.append(node.right)res.append(list(tmp))return res
这篇关于Leetcode:剑指Offer 32 - III从上到下打印二叉树IIII的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!