当前位置:网站首页>LeetCode 0116. Populate the next right node pointer for each node
LeetCode 0116. Populate the next right node pointer for each node
2022-07-19 08:39:00 【Tisfy】
【LetMeFly】116. Fill in the next right node pointer for each node
Force button topic link :https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/
Given a Perfect binary tree , All its leaf nodes are on the same layer , Each parent node has two children . A binary tree is defined as follows :
struct Node {
int val;
Node *left;
Node *right;
Node *next;
} Fill in each of its next The pointer , Let this pointer point to its next right node . If the next right node cannot be found , Will next The pointer is set to NULL.
In the initial state , all next The pointer is set to NULL.
Example 1:

Input :root = [1,2,3,4,5,6,7] Output :[1,#,2,3,#,4,5,6,7,#] explain : A given binary tree is shown in figure A Shown , Your function should fill in every one of its next The pointer , To point to its next right node , Pictured B Shown . The output of the serialization is arranged in sequence traversal , Nodes on the same layer are controlled by next Pointer connection ,'#' It marks the end of every layer .
Example 2:
Input :root = [] Output :[]
Tips :
- The number of nodes in the tree is
[0, 212 - 1]Within the scope of -1000 <= node.val <= 1000
Advanced :
- You can only use constant level extra space .
- Using recursion to solve problems also meets the requirements , In this problem, the stack space occupied by recursive program is not considered as additional space complexity .
Method 1 : Level traversal
In fact, this problem only needs to be binary tree Sequence traversal , Then record the last traversed node while traversing .
If this node is on the same level as the previous node , Then... Of the previous node next Point to this node .
The sequence traversal of binary tree can be referred to LeetCode 102. Sequence traversal of binary tree
- Time complexity O ( N ) O(N) O(N), among N N N Is the number of nodes
- Spatial complexity O ( M ) O(M) O(M), among M M M Is the number of nodes in the last layer
AC Code
C++
typedef pair<Node*, int> pii;
class Solution {
public:
Node* connect(Node* root) {
if (!root)
return root;
queue<pii> q;
q.push({
root, 1});
int lastLayer = 0;
Node* lastNode;
while (q.size()) {
auto[thisNode, thisLayer] = q.front();
q.pop();
if (thisLayer == lastLayer) {
lastNode->next = thisNode;
}
lastNode = thisNode;
lastLayer = thisLayer;
if (thisNode->left)
q.push({
thisNode->left, thisLayer + 1});
if (thisNode->right)
q.push({
thisNode->right, thisLayer + 1});
}
return root;
}
};
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125821076
边栏推荐
- 深度学习第三周Shallow Neural Networks习题整理
- WebGL的CPU负载问题-WebGL对比
- Collation of exercises of shallow neural networks in the third week of in-depth study
- 1. Decision tree
- JS learning notes 06-08: traversal of arrays and four methods of arrays
- HCIP --- OSPF的综合实验
- Solutions to license invalidation caused by MATLAB update
- Unity: window size adaptation when running on the browser after webgl Publishing
- Application of SCA on devsecops platform
- oop_引用类型变量传值
猜你喜欢

石墨厚度测量

Error received from peer ipv4/connection reset by peer paddleserving

微服务与微服务架构

深度学习之线性回归+基础优化

LeetCode 剑指 Offer II 041. 滑动窗口的平均值:低空间消耗解决

Redis

Redis常用数据类型——Redis列表(List)和Redis 集合(Set)

Graphite thickness measurement

Redis介绍

Matlab imports floating-point numbers with more than 9 digits after the decimal point
随机推荐
力扣1669合並兩個鏈錶筆記
事件循环、宏任务、微任务
Redis
Eureka Basics
最新一代互联网:WEB 3.0
instanceof的另类写法示例说明
Eureka基础知识
5.1 security vulnerabilities and Prevention
Talk about distributed locks
Nacos 新建配置管理
JS学习笔记09-12:原型对象以及Foreach+tostring及回收站
力扣1669合并两个链表笔记
MySQL data type
Openpyxl copy sheet pages across workbooks
RestTemplate
oop_引用类型变量传值
Li Kou 1669 merges two linked list notes
Consul服务注册与发现
1、决策树
Use torch NN builds the simplest neural network framework