Leetcode_116_Populating Next Right Pointers in Each Node
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43532817
Given a binary tree
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Note:
- You may only use constant extra space.
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1
/ \
2 3
/ \ / \
4 5 6 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
思路:
(1)题意为给定一颗二叉树(可以当做满二叉树),将其每层上的节点从左到右链接起来形成链表。
(2)本文主要使用队列来实现。大体思路为:从根节点开始,按从树根向下的顺序依次将每层的节点存入队列中。首先,将根节点存入队列中,只要队列不为空,就让队头元素出队,然后判断该元素是否有左右孩子,如果有分别将左右孩子加入队列中;然后,二叉树是满二叉树,所以可以求出每层的节点数目,依次为1,2,4,8,....,通过临时变量记录当前的层次level,通过Math.power(2, level),求得当前层上的节点数levelcount,还需要使用变量count记录当前层次上遍历了多少节点,这样每当遍历的节点数和levelcount相同时,说明当前层已遍历到最后一个节点,将该节点的next置为null,继续进行下一层的遍历。由于每次出队的总是第一个元素,所以只需将出队后的元素next置为当前队列第一个元素即可;循环遍历,直到所有节点进队列、出队列为止,最后所得即为结果。
(3)该题的思路和“按层次遍历二叉树”类似,感兴趣可以参照“按层次输出二叉树”。只不过本文所示算法效率不是很高,但是比较容易理解吧。
(4)希望本文对你有所帮助。
算法代码实现如下:
/**
*
* @author liqq
*/
public static void connect(TreeLinkNode root) {
if(root ==null) return ;
if(root!=null&&root.left==null&&root.right==null) root.next=null;
List<TreeLinkNode> list = new LinkedList<TreeLinkNode>();
list.add(root);
int count = 0;
int level = 0;
int levelcount = 1;
while(list.size()!=0){
TreeLinkNode fis = list.remove(0);
count++;
if(list.size()>=0 && count!=levelcount){
TreeLinkNode sec = list.get(0);
fis.next = sec;
}else{
fis.next =null;
level++;
levelcount = (int) Math.pow(2, level);
count=0;
}
if(fis.left!=null){
list.add(fis.left);
}
if(fis.right!=null){
list.add(fis.right);
}
}
}
Leetcode_116_Populating Next Right Pointers in Each Node的更多相关文章
- 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 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- [LeetCode] 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] Populating Next Right Pointers in Each Node 每个节点的右向指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- LEETCODE —— Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- LeetCode - Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- 【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 ...
- 【leetcode】Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
随机推荐
- python3中替换python2中cmp函数的新函数分析(lt、le、eq、ne、ge、gt)
本文地址:http://blog.csdn.net/sushengmiyan/article/details/11332589 作者:sushengmiyan 在python2中我们经常会使用cmp函 ...
- Singular value decomposition
SVD is a factorization of a real or complex matrix. It has many useful applications in signal proces ...
- 个人在AS的一些安卓适配经验
具体的安卓适配http://blog.csdn.net/qfanmingyiq/article/details/53219812 AS在屏幕适配方面做的比eclipse做得好得多. 以下AS中的一些具 ...
- 教你如何在Android 6.0上创建系统悬浮窗
郭霖大神的文章:http://mp.weixin.qq.com/s?__biz=MzA5MzI3NjE2MA==&mid=2650235949&idx=1&sn=0f7eded ...
- Jedis分片Sentinel连接池实验
Jedis分片Sentinel连接池实验 1.起因 众所周知,Redis官方HA工具Sentinel已经问世很久了,但令人费解的是,Jedis官方却迟迟没有更新它的连接池.到目前Maven库中最新的2 ...
- iOS 中捕获截屏操作
转自:iOS知识小集 在iOS 7后,苹果提供了UIApplicationUserDidTakeScreenshotNotification通知来告诉App用户做了截屏操作.苹果的描述如下: // T ...
- RxJava操作符(09-算术/聚合操作&连接操作)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51692493 本文出自:[openXu的博客] 目录: 算术聚合 Count Concat ...
- 使用redis构建文章投票系统
首先,我得说明这篇博客基本上就是<<redis in action>>第一章内容的读书笔记. 需求 首先,说明一下,我们的需求 用户可以发表文章,发表时,自己就默认的给自己的文 ...
- MPAndroidChart的K线图上添加均线
MPAndroidChart的K线图上添加均线 效果图 均线计算方法: 通常说的5日均线,10日均线,其实就是根据当前K线节点的时间维度来说的,当前每个节点代表一天,那么上面的均线就叫做日均线(几日均 ...
- BeanUtils Exception 之 FastHashMap
这里仅仅是为了记录一件十分奇怪的事情,在使用BeanUtils的过程中,所有的依赖包都添加了, common logging common collections ··· 在为boolean 这种基本 ...