当前位置:网站首页>[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
边栏推荐
- Halcon clolor_ pieces. Hedv: classifier_ Color recognition
- GEO数据库中搜索数据
- [daily training] 1200 Minimum absolute difference
- Program error record 1:valueerror: invalid literal for int() with base 10: '2.3‘
- ABC#237 C
- Basic number theory -- Euler function
- 2022.7.4-----leetcode.1200
- Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
- Some pitfalls of win10 network sharing
- 猜谜语啦(4)
猜你喜欢
Business modeling | process of software model
Xrosstools tool installation for X-Series
Redis implements a high-performance full-text search engine -- redisearch
Daily question - input a date and output the day of the year
Sword finger offer 09 Implementing queues with two stacks
猜谜语啦(8)
Sword finger offer 05 Replace spaces
STM32 lights up the 1.8-inch screen under Arduino IDE
Example 008: 99 multiplication table
Apaas platform of TOP10 abroad
随机推荐
Agile project management of project management
12、动态链接库,dll
Guess riddles (10)
Business modeling | process of software model
Business modeling of software model | overview
每日一题——输入一个日期,输出它是该年的第几天
Five design details of linear regulator
Example 006: Fibonacci series
暑假第一周
某公司文件服务器迁移方案
Sword finger offer 09 Implementing queues with two stacks
Esp8266 interrupt configuration
Business modeling of software model | vision
Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
Infix expression evaluation
Guess riddles (9)
Example 009: pause output for one second
Cmder of win artifact
Count of C # LINQ source code analysis
Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.