当前位置:网站首页>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
边栏推荐
- php存储密码
- 深度学习第二周Neural Network Basics习题整理
- scratch逆序输出 电子学会图形化编程scratch等级考试四级真题和答案解析2022年6月
- 力扣455分发饼干笔记
- Build an embedded development environment
- BCG 使用之CBCGPEdit控件
- How to set preferences when developing esp8266 and esp32 with Arduino
- ES6 learning function (strict mode, higher-order function, closure)
- 项目代码训练教程
- 创建静态库的基本步骤
猜你喜欢

创建静态库的基本步骤

百度Apoll

Redis

Microservices and microservice architecture

全志V3s学习记录(13)OV2640的使用

Application of SCA on devsecops platform

深度学习第三周Shallow Neural Networks习题整理

Database write Optimization: database and table segmentation and related issues

Basic steps for creating a static library

使用toruch.nn搭建最简单的神经网络骨架
随机推荐
Detailed explanation of ansible automatic operation and maintenance (IV) preparation, use, command execution and example demonstration of playbook in ansible
US pressure surges, tiktok changes global safety director
Paddleserving service deployment tensorrt reports an error, shape of TRT subgraph is [-1, -1768],
Wvppro-zlm-gb21818-camera
Xgen hair guide history cleared solution
Use of datasets in trochvision
ES6学习-函数(严格模式,高阶函数,闭包)
Snap 1669 combine deux notes de liste
深度学习第一周Introduction to Deep Learning习题整理
Openpyxl copy sheet pages across workbooks
#yyds干货盘点#Cross-origin 跨域请求
JS study note 04-05 - modification of constructor and creation using factory method
Redis新数据类型——Bitmaps
Enjoy JVM -- knowledge about GC garbage collection
Redis introduction
Nacos 新建配置管理
力扣43字符串相乘笔记
60. Initial knowledge of wsgiref handwritten web framework +jinja2 module
Eureka基础知识
力扣912排序数组笔记