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 ...
随机推荐
- oracle聚合函数及行专列,pivot rollup cube
1.原始数据 --方法-: --以单位分组,计算每类特殊情况的合计以及按照单位的小计数 with a as (SELECT b.szfz, case when tsqk is not null th ...
- java 虚函数
猜猜这里的代码输出的结果是多少? package test; public class ConstructorExample { static class Foo { int i; Foo() { i ...
- SignalR IOS Android
http://www.dotblogs.com.tw/toysboy21/archive/2014/03/24/144505.aspx https://www.youtube.com/watch?v= ...
- bitnami-redmineserver迁移
1. 背景 在Redmineserver迁移过程中.假设前后两个Redmine的版本号一样,事情就简单,假设版本号不一样,就有可能面临两个版本号数据库不兼容.那就比較麻烦了.本文旨在介绍数据库不兼容时 ...
- JS 16进制加密解密
http://www.zc520.cc/js/62.html <script type="text/javascript"> function JavaDe(){ va ...
- 不要把<a href="">当作按钮用
代码如下: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> < ...
- NerdTree 学习
http://www.jianshu.com/p/eXMxGx--------------来自大神到博客 到现在为止我仍然不能设置<F2>开启和隐藏目录树.sangxin.
- Javaweb开发中关于不同地方出现的绝对路径和相对路径
1.转发和包含路径 a)以“/”开头:相对当前项目路径,即默认为http://localhost:8080/项目名/ b)不以“/”开头:相对当前Servlet路径. eg:在Aservlet中写“B ...
- (译)Getting Started——1.3.4 Writing a Custom Class(编写自定义的类)
在开发IOS应用中,当你编写自定义的类时,你会发现很多的特殊场合.当你需要把自定义的行为和数据包装在一起时,自定义的类非常有用.在自定义的类中,你可以定义自己的存储.处理和显示数据的方法. 例如,I ...
- jquery.js:8672 Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.
html5谷歌流浪器报错:jquery.js:8672 Synchronous XMLHttpRequest on the main thread is deprecated because of i ...