Populating Next Right Pointers in Each Node(I and II)
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
将next设置为指向每一层的下一个节点。这样首先想到可以使用广度优先遍历(层序),也是对每一层处理,这里在处理每一层的元素时,只要不是一层的最后一个节点,它的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 ;
Queue<TreeLinkNode> q=new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
int size=q.size();
for(int i=0;i<size;i++){
TreeLinkNode node=q.poll();
if(node.left!=null) q.add(node.left);
if(node.right!=null) q.add(node.right);
if(i==size-1){ //每层的最后一个,将next指向null
node.next=null;
}else{//将指针指向下一次准备输出的节点.
node.next=q.peek();
}
}
} }
}
上面的方法很好,但是使用了一个额外的空间队列,这里不使用额外空间queue。
这里是深度优先遍历。观察可以看出,遍历每一层从左往右,给他们的下一层赋值next。具体看代码:
TreeLinkNode level_start=root;
while(level_start!=null){ //每一层的最左边
TreeLinkNode cur=level_start;//用于在一层中向右遍历
//给下一层赋值next
while(cur!=null){ if(cur.left!=null) cur.left.next=cur.right;
if(cur.right!=null&&cur.next!=null) cur.right.next=cur.next.left;//当next为空,则不作操作,默认cur.right.next=null
cur=cur.next;
}
level_start=level_start.left;
II
1
/ \
2 3
/ \ \
4 5 7
第二个题目是一样的,就是不是一颗满二叉树,是任意的。上面的队列方法还是一样可以做,代码一样。。而没用queue的方法我就不掌握了。
Populating Next Right Pointers in Each Node(I and II)的更多相关文章
- 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 ...
随机推荐
- 05 利用Appliction 传值Activity
步骤一:新建一个类继承Application必须是public class 不然直接奔溃 步骤二:在清单文件AndroidManifest.xml的application添加name属性 值为com. ...
- (NO.00005)iOS实现炸弹人游戏(二):素材选择的取舍
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 前面一篇里我们基本明确了游戏的大致玩法和特点.下面就游戏中会用到 ...
- Cocos2D iOS之旅:如何写一个敲地鼠游戏(十一):完善游戏逻辑
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- RTB--Real TimeBidding模式的互联网广告(实时竞价的广告投放)
RTB(real time bidding)实时竞价允许广告买家根据活动目标.目标人群以及费用门槛等因素对每一个广告及每次广告展示的费用进行竞价.竞价成功后获得广告展示机会,在展示位置上展示广告. 其 ...
- 《java入门第一季》之集合toString源码解析
代码: Collection c = new ArrayList(); c.add("hello"); c.add("world"); c.add(" ...
- 【一天一道LeetCode】索引目录 ---C++实现
[一天一道LeetCode]汇总目录 这篇博客主要收藏了博主所做题目的索引目录,帮助各位读者更加快捷的跳转到对应题目 目录按照难易程度:easy,medium,hard来划分,读者可以按照难易程度进行 ...
- [WinForm]dataGridView导出到EXCEL
方法一: SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "Execl files (*.xls)|*.xls"; ...
- python is not None
python 判空常用 XX is not None,但其实 not XX is None 也可以. http://stackoverflow.com/questions/2710940/pyth ...
- Css中的盒子结构padding和margin的区别
之前写过一个padding和marfgin的区别的博客见地址:http://blog.csdn.net/qq_32059827/article/details/50998965.那里只是笼统介绍了一下 ...
- 通过iframe引入另外一个项目中的html片段到项目中,解决样式,高度,兼容等问题的策略
<!--尾部开始--> <iframe src="http://172.16.24.11:9000/cartoon-web/footer_new" m ...