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 ...
随机推荐
- [开发笔记]-DataGridView控件中自定义控件的使用
最近工作之余在做一个百度歌曲搜索播放的小程序,需要显示歌曲列表的功能.在winform中采用DataGirdView来实现. 很久不写winform程序了,有些控件的用法也有些显得生疏了,特记录一下. ...
- 【第41套测试题NOIP2007】【排序】【DP】【高精度】【树】【图上路径】
先说点题外话,这两天的入学考试,炸了……语文有史以来最差,数学有史以来最差……还有4科,估计全炸……悲痛的心情,来调程序.这套题是8.31考的,从昨天晚上开始改的,因为第三题迟迟不想写,才拖到了现在. ...
- C- struct的使用
数组是二等公民,不能进行整体赋值,或者参数传递,或者作为返回值. But!如果封装在struct内部,就完全不一样了 #include <iostream> using namespace ...
- lucas 定理学习
大致意思就是求组合数C(n , m) % p的值, p为一个偶数 可以将组合数的n 和 m都理解为 p 进制的表示 n = ak*p^k + a(k-1)*p^(k-1) + ... + a1*p ...
- Right-BICEP 测试四则运算二程序
测试方法: Right-BICEP 测试计划: 1.Right-结果是否正确? 2.B-是否所有的边界条件都是正确的? 3.是否有乘除法? 4.是否有括号? 5.是否有输出方式? 6.是否可以选择出题 ...
- iOS开发之UITableView使用总结
什么是UITableView 在众多移动应用中,能看到各式各样的表格数据 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView UITableView继承自UIScrollVie ...
- java基础-001
一.区分final . finally . finalize 1.关键字final Java语言的关键字final可以用于变量.类或方法,但是含义会有所不同. -用于变量:一旦初始化,变量值就不能修 ...
- Struts2标签<s:textfield/>
中的name="",取值顺序是,如不加#,先从root中栈顶取,没有取到(即使栈顶有这个属性但是属性为空还是向下取,这点和el表达式不同)就去actioncontext的map中取 ...
- Listview没有优化之前
MainActivity.java package com.example.listviewdemo4; import java.util.ArrayList; import java.util.Ha ...
- 利用NTFS交换数据流隐藏文件
利用NTFS交换数据流隐藏文件 发表于 2012 年 12 月 15 日 由 晴刃 这篇文章介绍一下Windows的NTFS文件系统的ADS(alternate data streams,交换数据流) ...