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 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
这道题和上一道题的区别在于,上一道的树是满二叉树,这一个并不是。
还是先使用队列做了一次,ac但是速度并不是很快。
/**
* 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 ; Queue queue = new LinkedList<TreeLinkNode>(); queue.add(root); while( !queue.isEmpty() ){ int size = queue.size();
TreeLinkNode node1 = (TreeLinkNode) queue.poll();
if( node1.left != null )
queue.add(node1.left);
if( node1.right != null)
queue.add(node1.right);
if( size == 1)
continue;
TreeLinkNode node2 = (TreeLinkNode) queue.poll();
if( node2.left != null )
queue.add(node2.left);
if( node2.right != null)
queue.add(node2.right);
for( int i = 2;i<size;i++){
node1.next = node2;
node1 = node2;
node2 = ( TreeLinkNode ) queue.poll();
if( node2.left != null )
queue.add(node2.left);
if( node2.right != null)
queue.add(node2.right);
}
node1.next = node2;
} }
}
但是题目中要求是常数空间。
所以还需要修改。
记录上一行的开始和下一行的开始,然后依次改变next。
/**
* 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 ;
TreeLinkNode low = null ;//指的是下面一行的第一个结点
TreeLinkNode up = root;
if( root.left != null )
low = root.left;
else if( root.right != null )
low = root.right;
while( low != null ){
TreeLinkNode start = low;
TreeLinkNode upStart = up;
helper(start,upStart);
while( low != null ){
if( low.left != null ){
TreeLinkNode node = low.left;
up = low;
low = node;
break;
}
if( low.right != null ){
TreeLinkNode node = low.right;
up = low;
low = node;
break;
}
low = low.next;
} }
} public void helper(TreeLinkNode start,TreeLinkNode upStart){ if( upStart.left != null){
if( upStart.right != null){
start.next = upStart.right;
start = start.next;
}
}
upStart = upStart.next;
while( upStart != null ){ if( upStart.left != null ){
start.next = upStart.left;
start = start.next;
if( upStart.right != null ){
start.next = upStart.right;
start = start.next;
}
}else if( upStart.right != null ){
start.next = upStart.right;
start = start.next;
}
upStart = upStart.next;
}
}
}
leetcode 117 Populating Next Right Pointers in Each Node II ----- java的更多相关文章
- [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 ...
- 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
原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- [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 ...
随机推荐
- K2与OData和Swagger集成,从任何设备无需代码获取数据
K2近期宣布获得了DData和Swagger REST的支持,这件事情究竟有多好呢? K2与OData和Swagger的集成,保障K2 Blackpearl的用户能建立基于工作流和表单的解决方案,最重 ...
- python框架(flask/django/tornado)比较
一.对外数据接口 三者作为web框架,都是通过url映射对外的接口 flask:以decorator的形式,映射到函数中 django:以字典形式,映射到函数 tornado: 以字典形式,映射到类中 ...
- Processon 一款基于HTML5的在线作图工具
CSDN的蒋涛不久前在微博上评价说ProcessOn是web版的visio,出于好奇私下对ProcessOn进行了一番研究.最后发现无论是在用户体验上,还是在技术上,ProcessOn都比微软的Vis ...
- DataNode,NameNode,JobTracker,TaskTracker用jps查看无法启动解决办法
查看tasktracker的50060的地址无法正常查看,主要有两个原因,一个是在/tmp目录下有以前使用2.02版本留下的文件没有删除,二个是因为端口被占用了 解决方法: 一.删除/tmp目录下所有 ...
- C#特性
一.概念:C#的特性也是一个类,继承自System.Attribute,用于描述类.方法.字段等 二.类型: 2.1 .net中特性用来处理多种问题,比如序列化.程序的安全特性.防止即时编译器对 ...
- js面向对象(构造函数与继承)
深入解读JavaScript面向对象编程实践 Mar 9, 2016 面向对象编程是用抽象方式创建基于现实世界模型的一种编程模式,主要包括模块化.多态.和封装几种技术. 对JavaScript而言,其 ...
- 12-27cell常用的属性
1.创建cell // 创建一个cell并且设置cell的风格 UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UI ...
- Java中接口作为方法的返回
在<算法>中的散列表一节,在用拉链法实现散列表的API时要求实现以下一个方法: public Iterable<Key> keys() 我们知道Iterable是一个接口,那么 ...
- 爬虫学习----pattern
1.match match(string[, pos[, endpos]]) | re.match(pattern, string[, flags]): 这个方法将从string的pos下标处起尝试匹 ...
- apk在IIS中的MIME设置
支持下载的话: 扩展名中填写“.apk”, MIME类型中填写apk的MIME类型“ application/vnd.android.package-archive ”