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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. Leetcode#117 Populating Next Right Pointers in Each Node II

    原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...

  4. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  5. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  6. 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 ...

  7. 【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 ...

  8. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

  9. 【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 ...

随机推荐

  1. Error Code: 1055 incompatible with sql_mode=only_full_group_by

    OperationalError at / (1055, "Expression #1 of ORDER BY clause is not in GROUP BY clause and co ...

  2. Linux内核中等待队列的几种用法

    Linux内核里的等待队列机制在做驱动开发时用的非常多,多用来实现阻塞式访问,下面简单总结了等待队列的四种用法,希望对读者有所帮助. 1. 睡眠等待某个条件发生(条件为假时睡眠): 睡眠方式:wait ...

  3. 最小二乘法及C语言实现

    我们以最简单的一元线性模型来解释最小二乘法.什么是一元线性模型呢? 监督学习中,如果预测的变量是离散的,我们称其为分类(如决策树,支持向量机等),如果预测的变量是连续的,我们称其为回归.回归分析中,如 ...

  4. Failed to read artifact descriptor for avalon-framework:avalon-framewor

    在工程中,遇到了这个问题,百度了好久并没有满意的解决方案. 网上有一种办法是: 一.修改.m2/repository/avalon-framework/avalon-framework-api/里所有 ...

  5. AutoCAD如何设置A0A1图纸

    可以从网上下载相应的图纸模板,下载之后可以发现有相应的文字和模板文件   随后我们新建并找到这个dwt文件模板(比如要做一个A1的模板)   随后即可发现模板的样式,包括每种颜色的粗细,颜色和明细栏等 ...

  6. 性能指标 - OEE

    work center 是指 执行制造作业的资源, 可以是 一个人, 一组人, 一台自动机器, 一组自动机器, 一个半自动机器, 一组半自动机器, 或者是 一个区域组成的生产资源 基本参数 Time ...

  7. ID3算法Java实现

    ID3算法java实现 1 ID3算法概述 1.1 信息熵 熵是无序性(或不确定性)的度量指标.假如事件A的全概率划分是(A1,A2,...,An),每部分发生的概率是(p1,p2,...,pn).那 ...

  8. UDP通信注意事项

    今天调试UDP,笔记本上面可以实现但台式机上面竟然无法通信,后来找了半天,原来是权限问题.必须将用户权限设置为最低才行. 在运行里面输入UAC (user access control )用户权限设置 ...

  9. Linux内核中链表的学习

    一.自己学习链表 数组的缺点:(1)数据类型一致:(2)数组的长度事先定好,不能灵活更改. 从而引入了链表来解决数组的这些缺点:(1)结构体解决多数据类型(2)链表的组合使得链表的长度可以灵活设置. ...

  10. SQL Server -使用表触发器记录表插入,更新,删除行数

    1.如何使用sql获取当前session用户名和机器名 Select CURRENT_USER,Host_name() 2.如何在表触发器中获取当前表名称 SELECT OBJECT_SCHEMA_N ...