当前位置:网站首页>[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
边栏推荐
- [daily training] 1200 Minimum absolute difference
- Arduino operation stm32
- Reasons for the insecurity of C language standard function scanf
- Wheel 1:qcustomplot initialization template
- 猜谜语啦(2)
- 696. Count binary substring
- 猜谜语啦(6)
- leetcode - 445. Add two numbers II
- Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
- 【NOI模拟赛】汁树(树形DP)
猜你喜欢

Sword finger offer 06 Print linked list from end to end

Yolov4 target detection backbone

Agile project management of project management

Halcon color recognition_ fuses. hdev:classify fuses by color

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

Explore the authentication mechanism of StarUML

TypeScript手把手教程,简单易懂

图解八道经典指针笔试题

leetcode - 445. Add two numbers II

每日一题——替换空格
随机推荐
UE pixel stream, come to a "diet pill"!
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
MATLAB小技巧(28)模糊综合评价
Is the security account given by Yixue school safe? Where can I open an account
Hello everyone, welcome to my CSDN blog!
Arduino operation stm32
Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
皮尔森相关系数
Halcon wood texture recognition
每日一题——替换空格
JS asynchronous error handling
Numpy pit: after the addition of dimension (n, 1) and dimension (n,) array, the dimension becomes (n, n)
MATLAB小技巧(28)模糊綜合評價
Digital analog 2: integer programming
Sword finger offer 06 Print linked list from end to end
Business modeling of software model | overview
Old Wang's esp8266 and old Wu's ws2818 light strip
2022.7.4-----leetcode.1200
Arduino+a4988 control stepper motor
One dimensional vector transpose point multiplication np dot