当前位置:网站首页>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
边栏推荐
- The ODB model calculates the data and outputs it to excel
- Three. Based on ply format point cloud voxel model JS upload interface writing
- Some basic configurations in interlij idea
- PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)
- DTMF dual tone multi frequency signal simulation demonstration system
- 波场DAO新物种下场,USDD如何破局稳定币市场?
- 【PTA】整除光棍
- DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
- Experience of mathematical modeling in 18 year research competition
- DNS cloud school rising posture! Three advanced uses of authoritative DNS
猜你喜欢
【PTA】整除光棍
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
考研英语唐叔的语法课笔记
Recognition of high-speed road signs by Matlab using alexnet
【PTA】L1-002 打印沙漏
Some basic knowledge of devexpress report development
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
The ODB model calculates the data and outputs it to excel
Devexpress 14.1 installation record
随机推荐
Modeling based on catiav6
Es error: request contains unrecognized parameter [ignore_throttled]
Investigate why close is required after sqlsession is used in mybatties
PCL点云处理之直线与平面的交点计算(五十三)
16MySQL之DCL 中 COMMIT和ROllBACK
Automatically fill in body temperature and win10 task plan
LeetCode 709、转换成小写字母
Why does ES6 need to introduce map when JS already has object type
Use the rolling division method to find the maximum common divisor of two numbers
LeetCode 74、搜索二维矩阵
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
C migration project record: modify namespace and folder name
上海回應“面粉官網是非法網站”:疏於運維被“黑”,警方已立案
Click an EL checkbox to select all questions
Historical track data reading of Holux m1200-e Bluetooth GPS track recorder
R language uses timeroc package to calculate the multi time AUC value of survival data under competitive risk, uses Cox model and adds covariates, and R language uses the plotauccurve function of time
[latex] 5 how to quickly write out the latex formula corresponding to the formula
Browser - learning notes
LeetCode 232、用栈实现队列
Rédaction de thèses 19: différences entre les thèses de conférence et les thèses périodiques