当前位置:网站首页>Xiaohei's leetcode journey: 117. Fill the next right node pointer of each node II
Xiaohei's leetcode journey: 117. Fill the next right node pointer of each node II
2022-07-31 00:58:00 【little black invincible】
层次遍历法
""" # Definition for a Node. class Node: def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): self.val = val self.left = left self.right = right self.next = next """
class Solution:
def connect(self, root: 'Node') -> 'Node':
if not root:
return root
# 层次遍历
q = deque([root])
while q:
n = len(q)
pre_node = None
for i in range(n):
node = q.popleft()
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
if i != 0:
pre_node.next = node
pre_node = node
return root

使用已建立的 next 指针
""" # Definition for a Node. class Node: def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): self.val = val self.left = left self.right = right self.next = next """
class Solution:
def connect(self, root: 'Node') -> 'Node':
if not root:
return root
# Generate a linked list of child nodes
def handle(node):
# The linked list has the previous node
if self.last:
self.last.next = node
# 不存在上一个节点,则赋予nextStart
else:
self.nextStart = node
self.last = node
self.start = root
while self.start:
p = self.start
# 重制nextStart、last
self.nextStart = None
self.last = None
# The child node linked list is generated from the previous linked list
while p:
if p.left:
handle(p.left)
if p.right:
handle(p.right)
p = p.next
# next linked list starts
self.start = self.nextStart
return root

边栏推荐
- SereTOD2022 Track2代码剖析-面向半监督和强化学习的任务型对话系统挑战赛
- Jmeter参数传递方式(token传递,接口关联等)
- 人工智能与云安全
- 822. Walk the Grid
- Yolov7实战,实现网页端的实时目标检测
- MySQL的触发器
- unity2D横版游戏教程4-物品收集以及物理材质
- MySQL database advanced articles
- SereTOD2022 Track2 Code Analysis - Task-based Dialogue Systems Challenge for Semi-Supervised and Reinforcement Learning
- 小黑leetcode之旅:104. 二叉树的最大深度
猜你喜欢

Niuke.com question brushing training (4)

The client series of the DOM series

不用Swagger,那我用啥?

The difference between h264 and h265 decoding

MySQL master-slave replication and read-write separation script - pro test available

typescript16-void

MySQL triggers

Unity2D horizontal version game tutorial 4 - item collection and physical materials

Thesis understanding: "Designing and training of a dual CNN for image denoising"

一万了解 Gateway 知识点
随机推荐
The difference between h264 and h265 decoding
ShardingSphere之公共表实战(七)
【愚公系列】2022年07月 Go教学课程 017-分支结构之IF
ShardingSphere's unsharded table configuration combat (6)
ELK deployment script---pro test available
MySQL的触发器
MySQL table design for message queue to store message data
响应式布局与px/em/rem的比对
ShardingSphere之未分片表配置实战(六)
Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
ROS2系列知识(3):环境配置
【952. Calculate the maximum component size according to the common factor】
MySQL系列一:账号管理与引擎
Jetpack Compose learning (8) - State and remeber
人工智能与云安全
DOM系列之 offset 系列
MySQL master-slave replication and read-write separation script - pro test available
[Yugong Series] July 2022 Go Teaching Course 016-Logical Operators and Other Operators of Operators
Detailed explanation of 9 common reasons for MySQL index failure
BOM系列之history对象