[Leetcode][JAVA] Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
- You may only use constant extra space.
For example,
Given the following binary tree,
1
/ \
2 3
/ \ \
4 5 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL
这道题是Populating Next Right Pointers in Each Node的加强版,更适合一般情况,所以这道题的解法也适合Populating Next Right Pointers in Each Node。
对每一层,如果这一层节点已经连接好,那么可以通过next指针遍历这一层的所有节点,所以,也就可以按顺序遍历到这些节点的所有子节点,那么,也就可以按顺序将这些子节点连接。
问题是,连接好的子节点链头需要被记录下来,因为这样才能进行下一次遍历,将这些子节点的子节点们连接起来。
这里使用一个函数将这个功能封装起来,输入是待遍历层的头节点,子节点连接后,返回连接好的子节点链头(即下一层的头节点)。
函数中在遍历前新建一个辅助节点helper,helper的next指针指向第一个子节点(如果有的话)。这样,返回值就可以通过helper的next指针得到。
具体过程为,父层的遍历指针cur遍历到每个节点时,都讨论左子节点和右子节点。子层的遍历指针children从helper开始,遇到当前父层节点有左子就把当前children的next指针指向左子并把children指向左子。如果有右子也是一样。如此可以将所有子节点连接。
对所有层从上往下进行这样的函数调用,则可以把整个树每层子节点都连接起来。 注意,空间复杂度依然为O(1)因为函数中新建的辅助节点所占内存在出函数中会被释放。
代码:
public void connect(TreeLinkNode root) {
TreeLinkNode levelStart = root;
while(levelStart!=null)
levelStart = connectChildren(levelStart);
}
public TreeLinkNode connectChildren(TreeLinkNode root) {
TreeLinkNode cur = root;
TreeLinkNode helper = new TreeLinkNode(0);
TreeLinkNode children = helper;
while(cur!=null) {
if(cur.left!=null) {
children.next = cur.left;
children = children.next;
}
if(cur.right!=null) {
children.next = cur.right;
children = children.next;
}
cur = cur.next;
}
return helper.next;
}
[Leetcode][JAVA] Populating Next Right Pointers in Each Node II的更多相关文章
- [LeetCode] 117. Populating Next Right Pointers in Each Node II 每个节点的右向指针 II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
- 【leetcode】Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- Leetcode 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- Java for LeetCode 117 Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- leetcode 117 Populating Next Right Pointers in Each Node II ----- java
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- Leetcode 之Populating Next Right Pointers in Each Node II(51)
void connect(TreeLinkNode *root) { while (root) { //每一层循环时重新初始化 TreeLinkNode *prev = nullptr; TreeLi ...
- Leetcode#117 Populating Next Right Pointers in Each Node II
原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
随机推荐
- Windows 下动态链接库和静态链接库
1.静态链接库:就是在编译的时候把库中代码复制进工程中,导致工程变大,但是速度快. 缺点在于一套代码可能在内存中有多份拷贝,占用内存. 2.动态链接库:库由windos api加载库代码,内存中只有一 ...
- express 手动删除session状态(即登出功能)
在退出按钮被点击后,发送请求/logout,服务端做如下处理: app.get('/logout', function() { delete req.session.user; return res. ...
- notepad++ 正则表达式
body { font-family: Bitstream Vera Sans Mono; font-size: 11pt; line-height: 1.5; } html, body { colo ...
- 第九十九天上课 PHP TP框架 数据库查询和增加
在Model文件夹下创建模型,文件命名规则 : 表名Model.class.php <?php namespace Home\Model; use Think\Model; class yong ...
- IRunningObjectTable接口
IRunningObjectTable接口
- asp.net下拉列表绑定数据库多个字段
select ID, CONVERT(varchar(10),TBName) +','+RoomName+ ',最大人数:'+CONVERT(varchar(10),MAXNum) as ClassR ...
- HTTP请求之:PHP函数header常用功能
1.页面重定向 当浏览器接受到头信息中的 Location: http://xxxx 后,就会自动跳转到 http://xxxx 指向的URL地址,这点有点类似用 js 写跳转.但是这个跳转只有浏 ...
- poj 2987 最大权闭合图
Language: Default Firing Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 8744 Accept ...
- lucene query
在lucene的搜索中,最重要的无疑就是对query的理解和掌握了.这里扒拉一下源码(版本3.5.0)的query和query实现: query是一个抽象类,实现类有以下几个: termQuery m ...
- udp通信的原理---makefile文件
由于UDP通信不需要事先建立连接,因此不需要TCP中的connect函数. 服务器端的步骤如下: 1. socket: 建立一个socket 2. bind: 将这个soc ...