当前位置:网站首页>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
边栏推荐
- Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 64
- Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
- Leetcode dynamic planning training camp (1-5 days)
- ArcGIS js api 4. X submergence analysis and water submergence analysis
- 2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
- Some basic knowledge of devexpress report development
- WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
- DNS cloud school rising posture! Three advanced uses of authoritative DNS
- 如何做产品创新?——产品创新方法论探索一
- Thirty What are VM and VC?
猜你喜欢

Plato Farm元宇宙IEO上线四大,链上交易颇高

Recommend an open source free drawing software draw IO exportable vector graph

Matlab analytic hierarchy process to quickly calculate the weight

WordPress插件:WP-China-Yes解决国内访问官网慢的方法

Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)

考研英语唐叔的语法课笔记

Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model

PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)

DTMF双音多频信号仿真演示系统

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
随机推荐
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
堡垒机、跳板机JumpServer的搭建,以及使用,图文详细
Matlab analytic hierarchy process to quickly calculate the weight
After route link navigation, the sub page does not display the navigation style problem
Recognition of high-speed road signs by Matlab using alexnet
DNS cloud school | analysis of hidden tunnel attacks in the hidden corner of DNS
【PTA】L1-002 打印沙漏
I JS deep copy and shallow copy
redis 分布式锁
Leetcode dynamic planning training camp (1-5 days)
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
Development of Matlab GUI bridge auxiliary Designer (functional introduction)
如何做产品创新?——产品创新方法论探索一
Mysql database and table building: the difference between utf8 and utf8mb4
Monte Carlo py solves the area problem! (save pupils Series)
Thirty What are VM and VC?
Devexpress 14.1 installation record
Cadence Orcad Capture 批量更改元件封装功能介绍图文教程及视频演示
Why does ES6 need to introduce map when JS already has object type