Populating Next Right Pointers in Each Node II
来源:互联网 发布:制作地图的软件 编辑:程序博客网 时间:2024/05/17 03:46
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
我参考的:http://zjalgorithm.blogspot.com/2014/11/leetcode-java-populating-next-right.html
思路:constant space,还是跟上一题一样,从上到下,每层扫描并连接。
这题并不是什么先处理右边再处理左边,就是从上倒下,从左到右处理,只是用pre来记录要连接的前面一个点,如何赋值pre就要分pre == null和pre != null两种情况讨论。
这题跟上面的I很类似,也是每层访问,从左到右进行连接,访问下一层的时候,需要注意,因为这个时候,并不是满树,而是分左右节点为空,所以下一层需要额外讨论一下。这题需要注意的是,找下一层的开始点的时候,需要在内层循环里面找,因为可能上一层最左边的点的左右子树都是空的,但是后面的node有左右子树,所以需要在内部循环里面找下一层的起点。/** * 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 cur = root; while(cur!=null){ TreeLinkNode nextstart = null; TreeLinkNode pre = null; TreeLinkNode curlevel = cur; while(curlevel!=null){ if(nextstart == null){ nextstart = curlevel.left == null ? curlevel.right : curlevel.left; } if(curlevel.left!=null){ if(pre == null){ pre = curlevel.left; } else { pre.next = curlevel.left; pre = curlevel.left; } } if(curlevel.right!=null){ if(pre == null){ pre = curlevel.right; } else { pre.next = curlevel.right; pre = curlevel.right; } } curlevel = curlevel.next; } cur = nextstart; } } }
0 0
- Populating Next Right Pointers in Each Node、Populating Next Right Pointers in Each Node II
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- LeetCode Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- leetcode 79: Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node ii
- leetcode Populating Next Right Pointers in Each Node II
- 【leetcode】Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- [Leetcode]Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node (I & II)
- Populating Next Right Pointers in Each Node II
- 三轴加速度计的原理和方法
- [LeetCode] Unqiue Binary Search Trees II
- Converse帆布鞋 7mVo iEU n2obl
- 负载均衡技术之7LVS、Nginx和HAProxy对比总结
- ugg 8OHa U2oJ R3now
- Populating Next Right Pointers in Each Node II
- 2015人行计算机笔试回忆
- cocos2dx 无法从“void (__thiscall MyClass::* )(cocos2d::CCNode *)”转换为“cocos2d::SEL_MenuHandler”
- 圆桌对话:美女CTO是怎样炼成的?
- 廖晓格:让大数据更实时和可视化
- 【博客管理】博客资源汇总【置顶】
- android xml界面布局常用属性概括
- OpenGL中启用光照前的准备——指定法线
- 设计模式之验收总结