当前位置:网站首页>LeetCode 116. 填充每个节点的下一个右侧节点指针
LeetCode 116. 填充每个节点的下一个右侧节点指针
2022-04-23 20:23:00 【亡于灬】
116. 填充每个节点的下一个右侧节点指针
1)题目描述
给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
初始状态下,所有 next 指针都被设置为 NULL。
示例 1:

输入:root = [1,2,3,4,5,6,7]
输出:[1,#,2,3,#,4,5,6,7,#]
解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化的输出按层序遍历排列,同一层节点由 next 指针连接,'#' 标志着每一层的结束。
示例 2:
输入:root = []
输出:[]
提示:
- 树中节点的数量在
[0, 2^12 - 1]范围内 -1000 <= node.val <= 1000
2)分析
递归。
首先考虑根结点为空,直接返回空指针。
当根结点不为空的时候,考虑根结点有叶子结点。那么左叶子结点的next指向右叶子结点。
考虑当前根结点存在父结点,那么当前根结点一定有兄弟结点(参照FigureB中的2),那么当前根结点的右叶子结点(参照FigureB中的5)的next就应该指向当前根结点next所指结点(参照FigureB中的3)的左叶子结点(参照FigureB中的6)。
3)C++代码
/* // 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;
}
};
版权声明
本文为[亡于灬]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38342510/article/details/124360289
边栏推荐
- The second method of file upload in form form is implemented by fileitem class, servletfileupload class and diskfileitemfactory class.
- Mysql database backup scheme
- Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 64
- SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
- ABAQUS script email auto notification
- PostgreSQL basic functions
- 【PTA】L2-011 玩转二叉树
- How to do product innovation—— Exploration of product innovation methodology I
- Intersection calculation of straight line and plane in PCL point cloud processing (53)
- [PTA] get rid of singles
猜你喜欢

Five minutes to show you what JWT is

【PTA】整除光棍

After route link navigation, the sub page does not display the navigation style problem

堡垒机、跳板机JumpServer的搭建,以及使用,图文详细

Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report

Notes of Tang Shu's grammar class in postgraduate entrance examination English
![[stack and queue topics] - sliding window](/img/65/a2ce87f1401d7a28d4cce0cc85175f.png)
[stack and queue topics] - sliding window

What is the difference between a host and a server?

Rt-1052 learning notes - GPIO architecture analysis
![Es error: request contains unrecognized parameter [ignore_throttled]](/img/17/9131c3eb023b94b3e06b0e1a56a461.png)
Es error: request contains unrecognized parameter [ignore_throttled]
随机推荐
一. js的深拷贝和浅拷贝
SQL gets the latest record of the data table
Numpy sort search count set
R language uses econocrats package to create microeconomic or macroeconomic map, visualize indifference function indifference curve, customize calculation intersection, and customize the parameters of
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
论文写作 19: 会议论文与期刊论文的区别
ArcGIS js api 4. X submergence analysis and water submergence analysis
How can matlab obtain the truncated image in trainingimagelabeler
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
Automatically fill in body temperature and win10 task plan
Three. Based on ply format point cloud voxel model JS upload interface writing
网络通信基础(局域网、广域网、IP地址、端口号、协议、封装、分用)
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
JDBC database addition, deletion, query and modification tool class
Solution to PowerDesigner's failure to connect to MySQL in x64 system
Linux64Bit下安装MySQL5.6-不能修改root密码
How about CICC fortune? Is it safe to open an account
Change the material of unity model as a whole
An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue