当前位置:网站首页>[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
边栏推荐
- C语言标准函数scanf不安全的原因
- [nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
- Halcon clolor_ pieces. Hedv: classifier_ Color recognition
- js异步错误处理
- [牛客网刷题 Day4] JZ55 二叉树的深度
- Bit operation related operations
- MATLAB skills (28) Fuzzy Comprehensive Evaluation
- 整形的分类:short in long longlong
- Example 006: Fibonacci series
- 深度学习模型与湿实验的结合,有望用于代谢通量分析
猜你喜欢

猜谜语啦(9)

Sword finger offer 05 Replace spaces

猜谜语啦(5)

UE pixel stream, come to a "diet pill"!

C# LINQ源码分析之Count

Halcon blob analysis (ball.hdev)

Halcon clolor_ pieces. Hedv: classifier_ Color recognition

Arduino+a4988 control stepper motor
![[牛客网刷题 Day4] JZ35 复杂链表的复制](/img/bc/ce90bb3cb6f52605255f1d6d6894b0.png)
[牛客网刷题 Day4] JZ35 复杂链表的复制

Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
随机推荐
Cmder of win artifact
Halcon affine transformations to regions
Halcon clolor_ pieces. Hedv: classifier_ Color recognition
TypeScript手把手教程,简单易懂
猜谜语啦(6)
【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III
Business modeling | process of software model
Speech recognition learning summary
Tips 1: Web video playback code
Mathematical modeling: factor analysis
An enterprise information integration system
每日一题——替换空格
Task failed task_ 1641530057069_ 0002_ m_ 000000
Bluebridge cup internet of things basic graphic tutorial - GPIO input key control LD5 on and off
U8g2 drawing
Bit operation related operations
golang 基础 ——map、数组、切片 存放不同类型的数据
猜谜语啦(9)
Basic number theory -- Euler function
Run menu analysis