当前位置:网站首页>[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
边栏推荐
- Tips 1: Web video playback code
- Guess riddles (6)
- 暑假第一周
- Esphone Feixun DC1 soft change access homeassstant
- Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
- 每日一题——输入一个日期,输出它是该年的第几天
- 287. 寻找重复数-快慢指针
- 319. Bulb switch
- Guess riddles (5)
- 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?
猜你喜欢
Lori remote control commissioning record
Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
Arduino+a4988 control stepper motor
leetcode - 445. Add two numbers II
Lori remote control LEGO motor
Halcon clolor_ pieces. Hedv: classifier_ Color recognition
Example 010: time to show
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?
C# LINQ源码分析之Count
Mathematical modeling: factor analysis
随机推荐
【三层架构及JDBC总结】
Run menu analysis
MATLAB小技巧(28)模糊綜合評價
猜谜语啦(142)
Arduino+a4988 control stepper motor
猜谜语啦(11)
Hello everyone, welcome to my CSDN blog!
Guess riddles (10)
Classification of plastic surgery: short in long long long
Run菜单解析
Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
Basic number theory -- Euler function
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
Business modeling of software model | overview
IT冷知识(更新ing~)
Some pitfalls of win10 network sharing
696. 计数二进制子串
[牛客网刷题 Day4] JZ32 从上往下打印二叉树
C语言标准函数scanf不安全的原因
Guess riddles (7)