当前位置:网站首页>[牛客网刷题 Day4] JZ32 从上往下打印二叉树
[牛客网刷题 Day4] JZ32 从上往下打印二叉树
2022-07-05 08:39:00 【strawberry47】
题目
不分行从上往下打印出二叉树的每个节点,同层节点从左至右打印。例如输入{8,6,10,#,#,2,1},如以下图中的示例二叉树,则依次打印8,6,10,2,1(空节点不打印,跳过),请你将打印的结果存放到一个数组里面,返回。
思路
可以用队列的思路:
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
答案
答案里有递归的思路,还是搞不太明白是咋回事呢
import queue
class Solution:
def traverse(self, root: TreeNode, res: List[List[int]], depth: int):
if root:
# 新的一层
if len(res) < depth:
row = []
res.append(row)
row.append(root.val)
# 读取该层的一维数组,将元素加入末尾
else:
row = res[depth -1]
row.append(root.val)
else:
return
# 递归左右时深度记得加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)
#加入一维数组
for i in temp:
for j in i:
res.append(j)
return res
边栏推荐
- Esp8266 interrupt configuration
- 实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
- STM32 single chip microcomputer - external interrupt
- Apaas platform of TOP10 abroad
- 如何写Cover Letter?
- Guess riddles (4)
- Some pitfalls of win10 network sharing
- Business modeling of software model | stakeholders
- Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
- STM32 lights up the 1.8-inch screen under Arduino IDE
猜你喜欢

Daily question - input a date and output the day of the year

Example 008: 99 multiplication table

Business modeling of software model | object modeling

Count the number of inputs (C language)

Low code platform | apaas platform construction analysis

STM32 single chip microcomputer -- debug in keil5 cannot enter the main function

Keil use details -- magic wand
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?

实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

Bluebridge cup internet of things basic graphic tutorial - GPIO input key control LD5 on and off
随机推荐
STM32 --- serial port communication
2020-05-21
猜谜语啦(6)
Agile project management of project management
L298N module use
Reasons for the insecurity of C language standard function scanf
Weidongshan Internet of things learning lesson 1
UE pixel stream, come to a "diet pill"!
Warning: retrying occurs during PIP installation
STM32---IIC
Meizu Bluetooth remote control temperature and humidity access homeassistant
PIP installation
[three tier architecture and JDBC summary]
Run菜单解析
Esphone Feixun DC1 soft change access homeassstant
One question per day - replace spaces
STM32 single chip microcomputer - bit band operation
Low code platform | apaas platform construction analysis
Cmder of win artifact
Guess riddles (4)