当前位置:网站首页>LeetCode 116. Populate the next right node pointer for each node
LeetCode 116. Populate the next right node pointer for each node
2022-04-23 20:26:00 【Die in a trap】
116. Fill in the next right node pointer for each node
1) Title Description
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, 2^12 - 1]
Within the scope of -1000 <= node.val <= 1000
2) analysis
recursive .
First, consider that the root node is empty , Returns a null pointer directly .
When the root node is not empty , Consider that the root node has a leaf node . So the left leaf node next
Point to the right leaf node .
Consider that the current root node has a parent node , Then the current root node must have a sibling node ( reference FigureB
Medium 2
), Then the right leaf node of the current root node ( reference FigureB
Medium 5
) Of next
It should point to the current root node next
The point of reference ( reference FigureB
Medium 3
) Left leaf node of ( reference FigureB
Medium 6
).
3)C++
Code
/* // Definition for a Node. class Node { public: int val; Node* left; Node* right; Node* next; Node() : val(0), left(NULL), right(NULL), next(NULL) {} Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {} Node(int _val, Node* _left, Node* _right, Node* _next) : val(_val), left(_left), right(_right), next(_next) {} }; */
class Solution {
public:
Node* connect(Node* root) {
if(!root)
return NULL;
if(root->left){
root->left->next=root->right;
if(root->next)
root->right->next=root->next->left;
}
connect(root->left);
connect(root->right);
return root;
}
};
版权声明
本文为[Die in a trap]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232023107793.html
边栏推荐
- 黑客的入侵方式你知道几种?
- Imitation Baidu map realizes the three buttons to switch the map mode by automatically shrinking the bottom
- SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
- SQL gets the latest record of the data table
- 三十.什么是vm和vc?
- [latex] 5 how to quickly write out the latex formula corresponding to the formula
- LeetCode 232、用栈实现队列
- Some basic knowledge of devexpress report development
- LeetCode 709、转换成小写字母
- Experience of mathematical modeling in 18 year research competition
猜你喜欢
Commit and rollback in DCL of 16 MySQL
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
Rt-1052 learning notes - GPIO architecture analysis
On BIM data redundancy theory
BMP JPEG picture to vector image contourtrace
Matlab analytic hierarchy process to quickly calculate the weight
A useless confession artifact
CVPR 2022 | querydet: use cascaded sparse query to accelerate small target detection under high resolution
Wave field Dao new species end up, how does usdd break the situation and stabilize the currency market?
随机推荐
考研英语唐叔的语法课笔记
黑客的入侵方式你知道几种?
The second method of file upload in form form is implemented by fileitem class, servletfileupload class and diskfileitemfactory class.
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
【PTA】整除光棍
Common form verification
论文写作 19: 会议论文与期刊论文的区别
Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte
[PTA] l1-002 printing hourglass
三十.什么是vm和vc?
Devexpress 14.1 installation record
Numpy sort search count set
How do BIM swindlers cheat? (turn)
Implementation of mypromise
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
go-zero框架数据库方面避坑指南
The ODB model calculates the data and outputs it to excel
Experience of mathematical modeling in 18 year research competition
Notes of Tang Shu's grammar class in postgraduate entrance examination English
How can matlab obtain the truncated image in trainingimagelabeler