当前位置:网站首页>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
边栏推荐
- Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
- How does onlyoffice solve no route to host
- 黑客的入侵方式你知道几种?
- [PTA] l1-006 continuity factor
- Linux64Bit下安装MySQL5.6-不能修改root密码
- Why does ES6 need to introduce map when JS already has object type
- Es error: request contains unrecognized parameter [ignore_throttled]
- Browser - learning notes
- [talkative cloud native] load balancing - the passenger flow of small restaurants has increased
- 16MySQL之DCL 中 COMMIT和ROllBACK
猜你喜欢
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
The ODB model calculates the data and outputs it to excel
[stack and queue topics] - sliding window
Plato Farm元宇宙IEO上线四大,链上交易颇高
Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified
Servlet learning notes
[latex] 5 how to quickly write out the latex formula corresponding to the formula
Commit and rollback in DCL of 16 MySQL
Customize timeline component styles
Mysql database backup scheme
随机推荐
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
How about CICC fortune? Is it safe to open an account
[problem solving] 'ASCII' codec can't encode characters in position XX XX: ordinal not in range (128)
[graph theory brush question-4] force deduction 778 Swimming in a rising pool
Investigate why close is required after sqlsession is used in mybatties
Markdown < a > tag new page open link
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
Unity 模型整体更改材质
DNS cloud school rising posture! Three advanced uses of authoritative DNS
LeetCode 994、腐烂的橘子
Recognition of high-speed road signs by Matlab using alexnet
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
SQL gets the latest record of the data table
Identification of bolt points in aerial photography based on perception
Operation of numpy array
一. js的深拷贝和浅拷贝
I JS deep copy and shallow copy
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 64