当前位置:网站首页>[daiy4] jz32 print binary tree from top to bottom
[daiy4] jz32 print binary tree from top to bottom
2022-07-05 08:43:00 【strawberry47】
subject
Print each node of the binary tree from top to bottom without branches , The nodes of the same layer are printed from left to right . For example, the input {8,6,10,#,#,2,1}, As shown in the following figure, the example binary tree , Print in turn 8,6,10,2,1( Empty nodes do not print , skip ), Please store the printed results in an array , return .
Ideas
You can use the idea of queue :
from queue import Queue
class Solution:
def PrintFromTopToBottom(self , root: TreeNode):
# write code here
res = []
if root is None:
print("")
q = Queue()
q.put(root)
while not q.empty():
for i in range(q.qsize()):
node = q.get()
res.append(node.val)
if node.left:
q.put(node.left)
if node.right:
q.put(node.right)
return res
answer
There is a recursive idea in the answer , I still don't quite understand what's going on
import queue
class Solution:
def traverse(self, root: TreeNode, res: List[List[int]], depth: int):
if root:
# A new layer
if len(res) < depth:
row = []
res.append(row)
row.append(root.val)
# Read the one-dimensional array of this layer , Add the element to the end
else:
row = res[depth -1]
row.append(root.val)
else:
return
# Remember to add the depth when recursing left and right 1
self.traverse(root.left, res, depth + 1)
self.traverse(root.right, res, depth + 1)
def PrintFromTopToBottom(self , root: TreeNode) -> List[int]:
res = []
temp = []
if not root:
return res
self.traverse(root, temp, 1)
# Add one-dimensional array
for i in temp:
for j in i:
res.append(j)
return res
边栏推荐
- Business modeling of software model | overview
- GEO数据库中搜索数据
- 猜谜语啦(142)
- 每日一题——输入一个日期,输出它是该年的第几天
- Arduino+a4988 control stepper motor
- 多元线性回归(sklearn法)
- Guess riddles (11)
- Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
- [formation quotidienne - Tencent Selection 50] 557. Inverser le mot III dans la chaîne
- Example 010: time to show
猜你喜欢
随机推荐
How to manage the performance of R & D team?
Guess riddles (9)
How can fresh students write resumes to attract HR and interviewers
MATLAB skills (28) Fuzzy Comprehensive Evaluation
Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
12、动态链接库,dll
Example 007: copy data from one list to another list.
多元线性回归(sklearn法)
Shift operation of complement
Warning: retrying occurs during PIP installation
Example 008: 99 multiplication table
Matlab tips (28) fuzzy comprehensive evaluation
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
Reasons for the insecurity of C language standard function scanf
Sword finger offer 06 Print linked list from end to end
Guess riddles (2)
Sword finger offer 05 Replace spaces
Explore the authentication mechanism of StarUML
Guess riddles (7)
Sword finger offer 09 Implementing queues with two stacks







![[three tier architecture]](/img/73/c4c75a453f03830e83cabb0762eb9b.png)

