[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 ...
随机推荐
- 使用CSS和jQuery实现对话框
因为项目中要显示一些对话框,但用alert显得太丑,后从网上找了一些插件,但有觉得不好用,因此自己试用CSS和jQuery写了一个对话框,代码如下: <!DOCTYPE html> < ...
- uploadify图片上传发生Security Error
今天在使用uploadify进行图片上传的时候出现security error.其根本原因是flash跨域问题.主要原因是因为配了两个域名www.ttyouni.com 和 ttyouni.com 在 ...
- EXT学习之——EXT下拉框默认绑定第一个值
//默认第一个下拉框绑定值if (this.moduleCombo.store.getAt(0) != undefined) { this.moduleCombo.setValue(this.modu ...
- 2016年11月26号随笔(关于oracle数据库)
今天写了几个小时的sql语句,一开始我并没有思路,有思路便开始写. 首先我查询了入库表中的3级单位下的各个网点的入库信息,找到这些信息后,我又去入库明细表中查询入库的详细信息 找到了我要的把捆包箱的各 ...
- 移动端自动化环境搭建-JDK的安装
一.安装jdk A.安装依赖 JDK作为JAVA开发的环境,不管是做JAVA开发的学生,还是做安卓开发的同学,都必须在电脑上安装JDK. B.安装过程 安装JDK 选择安装目录 安装过程中会出现两次 ...
- IRunningObjectTable接口
IRunningObjectTable接口
- B站运维团队成长的血泪史
胡凯,bilibili运维负责人,曾经就职于金山软件.金山网络.猎豹移动,负责运维相关工作.Bilibili是国内最大的年轻人潮流文化娱乐社区,银河系知名弹幕视频分享UGC平台. 95后二次元新人 ...
- C++ 用libcurl库进行http通讯网络编程(转)
转载:http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三. ...
- .NET Remoting获取配置通道:
接上文: public static string ChannelManagerUrl { get { retu ...
- ios SourceTree中添加git项目工程文件
1.创建远程git仓库 2.复制远程仓库地址,最好选择http的地址. 3.在自己的电脑上下载一个SourceTree,然后在自己的电脑上建立链接. 点击左上角的+号桶开始添加,弄好点击Clone 4 ...