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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. Leetcode#117 Populating Next Right Pointers in Each Node II

    原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...

  4. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  5. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  6. 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 ...

  7. 【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 ...

  8. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

  9. 【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 ...

随机推荐

  1. 过滤器 Filter

    Filter(过滤器)简介 Filter 的基本功能是对发送到 Servlet 的请求进行拦截, 并对响应也进行拦截. Filter 程序是一个实现了 Filter 接口的 Java 类,与 Serv ...

  2. matlab:clear,close,clc

    clear 删除工作空间中的项目,释放系统内存 语法: clear clear name clear name1 name2 name3... clear global name clear -reg ...

  3. iOS多语言备选机制

    近期伊书突然接到一些外国用户的投诉,说伊书界面变成了中文,但他们系统语言不是中文,是法文俄文日文等,伊书只支持中文跟英文,在不支持系统所用语言的时候,理应会自动选用英文,不知为什么会选成了中文,经过艰 ...

  4. Struts2 validate校验

    一般的,用户注册的时候,我们需要校验一些用户提交过来的参数. 一般有两道屏障,一是在前台页面上使用js进行验证,直接杜绝了不正常信息的提交.二是将提交过来的信息进行验证,不通过则返回注册页面并显示错误 ...

  5. SpringMvc异常

    局部异常:在controller内部写一个处理异常的方法,注解ExceptionHandler(value={自己弄的异常class}) 这样发生value里面的类的异常,就可以执行这个方法,然后往r ...

  6. WebGis应用开发框架

    转自:http://www.cnblogs.com/zitsing/archive/2012/03/02/2377083.html 前言 Web Gis顾名思义就是通过浏览器方式操作的地理系统.通过浏 ...

  7. HD OJ2023

    #include "stdio.h"double stu[60],cla[10];int a[60][60];int main(){ int n,m,i,number,j; whi ...

  8. 属性的定义以及@synthesize的使用

    1.属性通常是指某些由对象封装或储存的数据.它可以是标志(如名称或颜色),也可以是与一个或多个其他对象的关系. 2.属性的基本声明使用 @property 编译器指令,后面紧跟属性的类型信息和名称.您 ...

  9. Add a stylesheet link programmatically in ASP.NET

    Here’s a code snippet used to programmatically insert a stylesheet link to an external CSS file: // ...

  10. Android.mk 常用宏和变量

    android ndk开发有一个重要的文件 Android.mk,他虽然重要,但是对它进行深入介绍的文档却比较的少,这里将对Android.mk中常用的宏和变量进行说明: 由于这一部分的内容多,资料零 ...