[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 ...
随机推荐
- Karma: 3 - 测试覆盖率
karma 的插件 karma-coverage 提供了测试代码覆盖率的支持. karma 的页面:http://karma-runner.github.io/0.8/config/coverage. ...
- iScroll滚动区域中select、input、textarea元素无法点击的Bug修复
最近在一个项目中使用了iScroll4模拟滚动效果,调试过程中发现一个表单页中的所有表单项都无法点击聚焦, 如<select>.<input>.<textarea> ...
- java se the operation is not applicable to the current selection
当新建某的类时,需要自动构建 set get方法时. 我们一般 会直接让Myeclipse自动生动.偶尔 .他会犯2. the operation is not applicable to the c ...
- Linux:基础配置--备忘
1.网络 /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 HWADDR=08:00:27:77:F3:A1 TYPE=Ethernet UU ...
- 【转】git push 出现401 错误
错误信息:error: The requested URL returned error: 401 Unauthorized while accessing https://git.oschina.n ...
- Git 版本控制 在 WIN 下的一些使用方法
这里记录一些 Git 在 Windows 操作系统下使用方法: 安装完毕后,先让Git 记录自己的名字: $ git config --global user.name "Your Name ...
- 8天掌握EF的Code First开发系列之2 简单的CRUD操作
本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 创建控制台项目 根据.Net中的类来创建数据库 简单的CRUD操作 数据库模式更改介绍 本章小结 本人的实验环境 ...
- [Reship]如何回复审稿人意见
================================= This article came from here:http://blog.renren.com/GetEntry.do?id= ...
- VC++ 在控件上写字时 字体的设置技巧
//人物照片下方的文字 CFont* nFont = &afxGlobalData.fontRegular; CFont* oFont = pDc->SelectObject(nFont ...
- SQL SERVER连接、合并查询
----创建测试表MyStudentInfoCREATE table MyStudentInfo( Id int not null primary key, Name varchar(16), ...