Leetcode-Populating Next Right Pointer in Binary Tree 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
Solution:
/**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void connect(TreeLinkNode root) {
if (root==null)
return; root.next = null;
TreeLinkNode levelHead = root; while (true){
boolean hasChild = false;
TreeLinkNode cur = levelHead;
TreeLinkNode nextLevelPre = null;
TreeLinkNode nextLevelHead = null;
while (cur!=null){
if (cur.left!=null){
hasChild = true;
if (nextLevelPre!=null){
nextLevelPre.next = cur.left;
nextLevelPre = cur.left;
} else {
nextLevelPre = cur.left;
nextLevelHead = cur.left;
}
} if (cur.right!=null){
hasChild = true;
if (nextLevelPre!=null){
nextLevelPre.next = cur.right;
nextLevelPre = cur.right;
} else {
nextLevelPre = cur.right;
nextLevelHead = cur.right;
}
} cur = cur.next;
}
if (hasChild)
nextLevelPre.next = null; //If reach the last level, then stop.
if (!hasChild)
break; //Move to next level.
levelHead = nextLevelHead;
} return;
}
}
At each level, after we construct the link list, we have a linked list to visit all nodes in this level. Then we can visit all child nodes in the next level along this linked list.This is a very smart way.
Note: For this question, we need to be careful about where the first child node in the next level is.
Leetcode-Populating Next Right Pointer in Binary Tree II的更多相关文章
- [Leetcode] Populating next right pointer in each node ii 填充每个节点的右指针
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- LeetCode: Populating Next Right Pointer in Each Node
LeetCode: Populating Next Right Pointer in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
- 【一天一道LeetCode】#104. Maximum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
- LeetCode Verify Preorder Serialization of a Binary Tree
原题链接在这里:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ 题目: One way to ...
- [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- LeetCode之104. Maximum Depth of Binary Tree
-------------------------------- 递归遍历即可 AC代码: /** * Definition for a binary tree node. * public clas ...
随机推荐
- 企业信息系统集成框架(设计思路)C++模式
设计要求: 1.企业信息系统框架.第三方产品通过接口层进行分层. 2.企业信息系统框架如何自由的继承第三方产品:通过一个抽象类.(软件设计要求:模块要求松,接口要求紧). 设计步骤: 1.报文的接受与 ...
- Atitit.异常的设计原理与 策略处理 java 最佳实践 p93
Atitit.异常的设计原理与 策略处理 java 最佳实践 p93 1 异常方面的使用准则,答案是:: 2 1.1 普通项目优先使用异常取代返回值,如果开发类库方面的项目,最好异常机制与返回值都提供 ...
- 利用JMX统计远程JAVA进程的CPU和Memory
http://songzi0206.iteye.com/blog/1541636 ******************** 从JAVA 5开始,JDK提供了一些JVM检测的API,这就是有名的java ...
- oracle中exists 和 in 的区别
1)用IN select * from A where id in(select id from B); 以上查询使用了in语句,in()只执行一次,它查出B表中的所有id字段并缓存起来.注意,是缓存 ...
- oracle序列在insert into 语句中的使用
很多人创建了序列,但是在插入语句中不知道怎么使用,在此做个简单介绍. oracle序列有两个参数:nextval和currval,使用的时候,需要输入sequence_name.nextval或seq ...
- CentOS平滑升级Nginx
服务器:CentOS 6.4 64位 升级方案:nginx1.4.0 – nginx1.4.3 Nginx编译后就一个小文件,不带动态库,升级也可以无缝升级,并不影响访问,按下面的命令执行就可以,具体 ...
- php安装redis扩展初始化失败解决办法
错误信息如下: PHP Warning: PHP Startup: redis: Unable to initialize module Module compiled with module API ...
- windows 短文件名/短路径名规则
How Windows Generates 8.3 File Names from Long File Names Windows generates short file names from lo ...
- Handler vs Timer,究竟该用哪个?
Handler vs Timer 在我们Android开发过程中,经常需要执行一些短周期的定时任务,这时候有两个选择Timer或者Handler.然而个人认为:Handler在多个方面比Timer更为 ...
- EasyUI的window加载的页面不执行js问题说明
http://364434006.iteye.com/blog/1671907 ———————————————————————————————————————————————————————————— ...