当前位置:网站首页>LeetCode116. Populate the next right node pointer for each node
LeetCode116. Populate the next right node pointer for each node
2022-06-28 21:05:00 【Yuyy】
This paper is finally updated at 484 Days ago, , The information may have developed or changed .
One 、 Ideas
after drawing Find out , The connection between sons is OK . But the grandchildren of different fathers have problems connecting , It's OK to solve one , The trick is to recurse , Can solve all these problems . Always thinking about solving problems Divide and conquer , But after the partition, I didn't expect to restore it ...
Two 、 problem
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.
Advanced :
- You can only use constant level extra space .
- Using recursion to solve problems also meets the requirements , In this problem, the stack space occupied by recursive program is not considered as additional space complexity .
Example :
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 .Tips :
- The number of nodes in the tree is less than
4096 -1000 <= node.val <= 1000
Related Topics
- Trees
- Depth-first search
- Breadth first search
\n
- 404
- 0
3、 ... and 、 Code
public Node connect(Node root) {
if (root == null) {
return null;
}
convert(root.left, root.right);
return root;
}
private void convert(Node left, Node right) {
if (left == null || right == null) {
return;
}
left.next = right;
convert(left.left, left.right);
convert(right.left, right.right);
convert(left.right, right.left);
}Post Views: 297
边栏推荐
猜你喜欢

【Try to Hack】Cobalt Strike(一)

LeetCode每日一题——515. 在每个树行中找最大值

MySQL system error occurred 1067

pyechart绘制多条y轴折线图

Ehcache配置资料,方便自己查
![[try to hack] cobalt strike (I)](/img/2b/5d274078b7d7ebd05b7c6d9e020868.png)
[try to hack] cobalt strike (I)

视频号如何下载视频?来看超简单方法!
![[graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!](/img/e5/b6035abfa7d4bb59c3080d3b87ce45.jpg)
[graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!

方 差 分 析

图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer
随机推荐
二叉树类题目 力扣
Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
【读书会第13期】视频文件的封装格式
Application of Andy s first dictionary (uva10815) Purple Book p112set
RT thread thread synchronization and thread communication
APISIX 助力中东社交软件,实现本地化部署
LeetCode213. House raiding II
How to analyze the relationship between enterprise digital transformation and data asset management?
T-test (test whether the mean difference between the two populations is significant)
Various types of long
Ehcache配置资料,方便自己查
软件watchdog和ANR触发memory dump讲解
postman简介与安装步骤
LeetCode560. 和为K的子数组
Ref attribute, props configuration, mixin mixing, plug-in, scoped style
大智慧上怎么进行开户啊, 安全吗
Leetcode 36. Effective Sudoku (yes, once)
MySQL system error occurred 1067
视频号如何下载视频?来看超简单方法!
Keyword long