当前位置:网站首页>小黑leetcode之旅:117. 填充每个节点的下一个右侧节点指针 II
小黑leetcode之旅:117. 填充每个节点的下一个右侧节点指针 II
2022-07-31 00:52:00 【小黑无敌】
层次遍历法
""" # 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
# 生成孩子节点链表
def handle(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
# 通过上一层链表生成孩子节点链表
while p:
if p.left:
handle(p.left)
if p.right:
handle(p.right)
p = p.next
# 下一个链表开始
self.start = self.nextStart
return root

边栏推荐
- 【愚公系列】2022年07月 Go教学课程 017-分支结构之IF
- ShardingSphere之读写分离(八)
- [Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes
- 华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
- Responsive layout vs px/em/rem
- What is Promise?What is the principle of Promise?How to use Promises?
- typescript13 - type aliases
- Consistency and Consensus of Distributed Systems (1) - Overview
- typescript13-类型别名
- MySQL table design for message queue to store message data
猜你喜欢

WEB Security Basics - - - Vulnerability Scanner
![[In-depth and easy-to-follow FPGA learning 13---------Test case design 1]](/img/1c/a88ba3b01d2e2302c26ed5f730b956.png)
[In-depth and easy-to-follow FPGA learning 13---------Test case design 1]

MySQL数据库进阶篇

Error occurred while trying to proxy request The project suddenly can't get up

MySQL高级-六索引优化

ShardingSphere read-write separation (8)

typescript15- (specify both parameter and return value types)

The difference between h264 and h265 decoding

ECCV 2022丨轻量级模型架构火了,力压苹果MobileViT(附代码和论文下载)

系统设计.短链系统设计
随机推荐
MySQL Series 1: Account Management and Engine
xss bypass: prompt(1)
Oracle has a weird temporary table space shortage problem
background has no effect on child elements of float
TypeScript在使用中出现的问题记录
华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
typescript13 - type aliases
【952. Calculate the maximum component size according to the common factor】
Rocky/GNU之Zabbix部署(3)
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
Neural Network (ANN)
Thesis understanding: "Designing and training of a dual CNN for image denoising"
typescript10-常用基础类型
typescript14-(单独指定参数和返回值的类型)
ELK deployment script---pro test available
typescript17-函数可选参数
这个项目太有极客范儿了
Meeting OA project pending meeting, all meeting functions
Jmeter parameter transfer method (token transfer, interface association, etc.)
[C language course design] C language campus card management system