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 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
解题思路一:
本题的难点在于需要逐层遍历才行,因此可以用Java for LeetCode 102 Binary Tree Level Order Traversal的思路,JAVA实现如下:
public void connect(TreeLinkNode root) {
if (root == null || (root.left == null && root.right == null))
return;
List<List<TreeLinkNode>> list=new ArrayList<List<TreeLinkNode>>();
Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();
queue.add(root);
while (queue.size() != 0) {
List<TreeLinkNode> alist = new ArrayList<TreeLinkNode>();
for (TreeLinkNode child : queue)
alist.add(child);
list.add(new ArrayList<TreeLinkNode>(alist));
Queue<TreeLinkNode> queue2=queue;
queue=new LinkedList<TreeLinkNode>();
for(TreeLinkNode child:queue2){
if (child.left != null)
queue.add(child.left);
if (child.right != null)
queue.add(child.right);
}
}
for(List<TreeLinkNode> alist:list)
for(TreeLinkNode aNode:alist)
connectARoot(aNode);
}
public static void connectARoot(TreeLinkNode root){
if (root == null || (root.left == null && root.right == null))
return;
if (root.next == null) {
if (root.left != null)
root.left.next = root.right;
}
else if (root.right == null) {
TreeLinkNode temp=root.next;
while(temp!=null){
if(temp.left==null&&temp.right==null)
temp=temp.next;
else break;
}
if(temp!=null)
root.left.next = (temp.left == null?temp.right:temp.left);
}else {
if (root.left != null)
root.left.next = root.right;
TreeLinkNode temp=root.next;
while(temp!=null){
if(temp.left==null&&temp.right==null)
temp=temp.next;
else break;
}
if(temp!=null)
root.right.next = (temp.left == null?temp.right:temp.left);
}
}
解题思路二:
不使用队列,请移步Populating Next Right Pointers in Each Node I II@LeetCode
Java for LeetCode 117 Populating Next Right Pointers in Each Node II的更多相关文章
- [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 ...
- 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 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 ...
随机推荐
- hdu1874最短路
裸裸的最短路问题,将while(scanf("%d%d", &N, &M)!=EOF)粗心写为while(scanf("%d%d", & ...
- 【开发总结】—— BABYLON 项目开发必备系列
前言:在公司主要使用Babylon.js作为前端引擎,我自己在开发中总结到基本上每一个新项目都会需要这些基本设置. 一.ios兼容:解决镜面反射 如果不加offline,材质中有镜面反射,苹果手机 ...
- mysql下监测数据库语句creating sort index时间过长的问题
在一张单表5000W数据上进行数据查询时传入两个单列索引条件,进行组合索引查询时,如果最后有order by id排序,与去除该排序,性能差距接近两个数量级 结论:在使用列的默认排序时,不应该再ord ...
- 【秀优越(xie e)】原来出题也能够这么恶心。
通过邪恶的数据范围和数据限制居然能够把一道传统题出成题答2333. 诶毕竟内部互測,题目就不往上贴了. 特殊限制 - - - 题目作废.输出M行"Orz PoPoQQQ" - M ...
- 【Redmine】Redmine 3.0.1 安装与配置
Redmine安装 VM安装Linux 安装Bitnami Redmine 配置环境 1.VM安装Linux 使用虚拟机安装Linux 本文使用的是Centos(CentOS-6.3-x86_64-b ...
- 伪静态对struts action的重写
参见 http://ocaicai.iteye.com/blog/1312189 最重要的而是在web.xml中配置 <filter-mapping> <filter-name> ...
- Jquery.ajax报parseerror Invalid JSON错误的原因和解决方法:不能解析
(默认: 自动判断 (xml 或 html)) 请求失败时调用时间.参数有以下三个:XMLHttpRequest 对象.错误信息.(可选)捕获的错误对象.如果发生了错误,错误信息(第二个参数)除了得到 ...
- 数组方式使用jQuery对象
一. 使用jQuery选择器获取结果是一个jQuery对象.然而,jQuery类库会让你感觉你正在使用一个定义了索引和长度的数组.在性能方面,建议使用简单的for或者while循环来处理,而不是$.e ...
- (十)jQuery对表单、表格的操作
一.表单应用 1.HTML中的表单大致由三部分组成 (1).表单标签:包含处理表单数据所用的服务端程序URL,以及数据提交到服务器的方法. (2).表单域:包含文本框.密码框.隐藏域.多行文本框.复选 ...
- RGB颜色空间alpha混合的方法
http://blog.csdn.net/xhhjin/article/details/6444782http://blog.csdn.net/xhhjin/article/details/64454 ...