当前位置:网站首页>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
边栏推荐
- PostgreSQL basic functions
- Computing the intersection of two planes in PCL point cloud processing (51)
- The second method of file upload in form form is implemented by fileitem class, servletfileupload class and diskfileitemfactory class.
- 网络通信基础(局域网、广域网、IP地址、端口号、协议、封装、分用)
- Es error: request contains unrecognized parameter [ignore_throttled]
- Commit and ROLLBACK in DCL of 16mysql
- BMP JPEG 图片转换为矢量图像 ContourTrace
- Experience of mathematical modeling in 18 year research competition
- 6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
- Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
猜你喜欢
Es error: request contains unrecognized parameter [ignore_throttled]
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
【PTA】整除光棍
Numpy Index & slice & iteration
On BIM data redundancy theory
PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)
Installation and use of NVM
How can matlab obtain the truncated image in trainingimagelabeler
Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
Numpy - creation of data type and array
随机推荐
[PTA] l1-002 printing hourglass
JDBC database addition, deletion, query and modification tool class
論文寫作 19: 會議論文與期刊論文的區別
Record: call mapper to report null pointer Foreach > the usage of not removing repetition;
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
XXXI` Prototype ` displays prototype properties and`__ proto__` Implicit prototype properties
[stack and queue topics] - sliding window
RT-1052学习笔记 - GPIO架构分析
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
Mysql database backup scheme
Es error: request contains unrecognized parameter [ignore_throttled]
[problem solving] 'ASCII' codec can't encode characters in position XX XX: ordinal not in range (128)
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
Handwritten Google's first generation distributed computing framework MapReduce
Notes of Tang Shu's grammar class in postgraduate entrance examination English
redis 分布式锁
Wave field Dao new species end up, how does usdd break the situation and stabilize the currency market?
ABAQUS script email auto notification
三十.什么是vm和vc?
堡垒机、跳板机JumpServer的搭建,以及使用,图文详细