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 ...
随机推荐
- Mac环境下svn命令行的使用
在Windows环境中,我们一般使用TortoiseSVN来搭建svn环境.在Mac环境下,由于Mac自带了svn的服务器端和客户端功能,所以我们可以在不装任何第三方软件的前提下使用svn功能,不过还 ...
- Drawable 添加过滤色,改变图片颜色
/** * 更改图片颜色 * @param drawable * @param color * @return */ public Drawable getDrawable(Drawable draw ...
- 做dg时遇到的log_archive_dest、log_archive_dest_1、db_recovery_file_dest之间互相影响
前提:归档开启.默认不指定归档文件夹. 今晚遇到客户那里设置了闪回区和log_archive_dest.不停库做DG时,无法指定log_archive_dest_n參数,巨坑. .实验了下.结论例如以 ...
- Python setup.py和MANIFEST.in文件
Setup.py文件 from setuptools import setup from codecs import open # 第三方依赖包及版本号 requires = ['beautifuls ...
- hibernate映射排序
@OneToMany(mappedBy="member") @OrderBy(value = "TousuID desc")
- hive分区(partition)简介
一.背景 1.在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据,因此建表时引入了partition概念. 2.分区表指的是在创建表 ...
- android历史
Android一词最早是出如今法国作家维里耶德利尔·亚当1986年发表的<未来夏娃>这部科幻小说中,作者利尔·亚当将外表像人类的机器起名为Android.这就是Android小人名字的由来 ...
- swiper的理解
参考:Swiper中文网 Swiper使用方法: <!DOCTYPE html> <html> <head> <meta charset="UTF- ...
- java集合归纳
学习自: http://android.blog.51cto.com/268543/400557/ MAP Collection 堆栈队列操作尽可能考虑 linkedlist 多线程同步操作尽可能考虑 ...
- ORACLE物化视图具体解释
一.物化的一般使用方法物化视图是一种特殊的物理表,"物化"(Materialized)视图是相对普通视图而言的.普通视图是虚拟表.应用的局限性大,不论什么对视图的查询.oracle ...