当前位置:网站首页>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
边栏推荐
- 2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
- Still using listview? Use animatedlist to make list elements move
- Linux64Bit下安装MySQL5.6-不能修改root密码
- JDBC database addition, deletion, query and modification tool class
- Identification of bolt points in aerial photography based on perception
- An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue
- . Ren -- the intimate artifact in the field of vertical Recruitment!
- selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
- 論文寫作 19: 會議論文與期刊論文的區別
- LeetCode 542、01 矩阵
猜你喜欢
[graph theory brush question-4] force deduction 778 Swimming in a rising pool
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
Shanghai responded that "flour official website is an illegal website": neglect of operation and maintenance has been "hacked", and the police have filed a case
BMP JPEG 图片转换为矢量图像 ContourTrace
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
Operation of numpy array
Five minutes to show you what JWT is
Recognition of high-speed road signs by Matlab using alexnet
Identification of bolt points in aerial photography based on perception
[PTA] l1-002 printing hourglass
随机推荐
三十.什么是vm和vc?
Scripy tutorial - (2) write a simple crawler
论文写作 19: 会议论文与期刊论文的区别
Experience of mathematical modeling in 18 year research competition
[latex] 5 how to quickly write out the latex formula corresponding to the formula
【栈和队列专题】—— 滑动窗口
JDBC tool class jdbcconutil gets the connection to the database
Es error: request contains unrecognized parameter [ignore_throttled]
RT-1052学习笔记 - GPIO架构分析
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
Modeling based on catiav6
堡垒机、跳板机JumpServer的搭建,以及使用,图文详细
Commit and rollback in DCL of 16 MySQL
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
Development of Matlab GUI bridge auxiliary Designer (functional introduction)
[problem solving] 'ASCII' codec can't encode characters in position XX XX: ordinal not in range (128)
Recommend an open source free drawing software draw IO exportable vector graph
Browser - learning notes
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 64
. Ren -- the intimate artifact in the field of vertical Recruitment!