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 ...
随机推荐
- Windows任务计划
任务计划,可以将任何脚本.程序或文档安排在某个时间运行.“任务计划”在每次启动windows系统的时候自动启动(默认Task Scheduler服务是开启的)并在后台运行.使用“任务计划”可以完成以下 ...
- 如何获取google可以访问的IP地址
由于某些原因,google的部分网站无法打开,导致我们的好些资源都无法找到,今天在网上看到一篇文件,教大家如何能找到可以访问的google. 假如我们需要访问的是:https://code.googl ...
- Linux文件管理系统
首先了解一般linux文件系统的构成. */usr/bin./bin : 存放所有用户可以执行的命令 */usr/sbin ./sbin :存放只有root可以执行的命令 */home : 用户缺省宿 ...
- jQuery 元素遍历
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- HTML--2图片热点,网页划区,拼接
图片热点: 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. 示例: 网页划区: 在一个网页里,规划出一个区域用来展示另一个网页的内容. 示例: 网页的拼接: 在一个网络 ...
- 《java作业》
/* 2.编写一个类,该类有一个方法public int f(int a,int b), 该方法返回a和b的最大公约数.然后再编写一个该类的子类, 要求子类重写方法f,而且重写的方法将返回a和b的最小 ...
- 一篇文章教你学会基础的HTML
html是学习做网页的基础,漂亮的网页与布局就是由有些html代码组成,大家看完这篇文章就可以简单的了解html了,多写多练 如果你不致力于成为美工的话,那么作为开发人员,可以读懂HTML.必 ...
- hdu 2048
PS:WA了两次...主要是没注意到fac的大小好像只能写到9...要用long long型递归求阶乘... 然后就是错排公式...百度下.. 代码: #include "stdio.h&q ...
- WEB ui快速构建
http://www.runoob.com/bootstrap/bootstrap-ui-editor.html 1http://pingendo.com/ 2http://www.layoutit. ...
- "由于这台计算机没有远程桌面客户端访问许可证,远程会话被中断"的解决方案
先使用如下命令登录到服务器: mstsc /v:{服务器IP} /admin 然后再使用下列方法之一即可. 方法一: 1.单击“开始→运行”,输入“gpedit.msc”打开组策略编辑器窗口,依次定位 ...