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

原题链接:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/

题目:前一题的继续。假设给定的是随意的二叉树。前题的代码还能用吗?

思路:显然,前一题不能用了。仅仅好用层序遍历。

	public void connect(TreeLinkNode root) {
if(root == null)
return;
TreeLinkNode parent = root,pre,next;
while(parent != null){
pre = null;
next = null;
while(parent != null){
if(next == null)
next = (parent.left == null) ? parent.right : parent.left;
if(parent.left != null){
if(pre != null){
pre.next = parent.left;
pre = pre.next;
}else
pre = parent.left;
}
if(parent.right != null){
if(pre != null){
pre.next = parent.right;
pre = pre.next;
}else
pre = parent.right;
}
parent = parent.next;
}
parent = next;
}
}

LeetCode——Populating Next Right Pointers in Each Node II的更多相关文章

  1. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

    Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...

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

  3. [leetcode]Populating Next Right Pointers in Each Node II @ Python

    原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...

  4. LeetCode - Populating Next Right Pointers in Each Node II

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

  5. [LeetCode] [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 ...

  6. LeetCode:Populating Next Right Pointers in Each Node I II

    LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...

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

  8. [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

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

随机推荐

  1. Android NFC传输联系人VCF

    import android.app.Activity; import android.content.ContentResolver; import android.content.Context; ...

  2. BZOJ 2005: [Noi2010]能量采集( 数论 + 容斥原理 )

    一个点(x, y)的能量损失为 (gcd(x, y) - 1) * 2 + 1 = gcd(x, y) *  2 - 1. 设g(i)为 gcd(x, y) = i ( 1 <= x <= ...

  3. 几种经典的数据排序及其Java实现

    选择排序 思想 n个记录的文件的直接选择排序可经过n-1趟直接选择排序得到有序结果: ①初始状态:无序区为R[1..n],有序区为空. ②第1趟排序 在无序区R[1..n]中选出关键字最小的记录R[k ...

  4. AOP编程,spring实现及JDK,CGLIB实现

    什么是AOP? AOP(Aspect-OrientedProgramming,面向方面编程)和OOP(Object-Oriented Programing,面向对象编程)思想不同,两者并非对立关系,前 ...

  5. Delphi中关于Rtti的一些操作(一)

    function TForm1.ShowMethodAddress(aObj: TDerived; const sData: String) : Pointer;var  aPtr : Pointer ...

  6. html5 canvas 实现一个简单的叮当猫头部

    原文:html5 canvas 实现一个简单的叮当猫头部 html5的canvas是很强大的,今天也是温习了一下之前的基础知识,然后学着做了一个简单的小案例.虽然在这一块几乎空白,但还是乐于尝试... ...

  7. C#日志工具汇总

    log4net          log4net是一个可以帮助程序员把日志信息输出到各种不同目标的.net类库.它可以容易的加载到开发项目中,实现程序调试和运行的时候的日志信息输出,提供了比.net自 ...

  8. eclipse插件maven的使用,web打包成WAR,tomcat下直接运行

    1.首先下载maven  其下载地址为:http://maven.apache.org/download.html   下载apache-maven-3.0.3-bin.zip 环境变量配置为  变量 ...

  9. DM6446开发攻略——u-boot-1.3.4移植(1)

    http://zjbintsystem.blog.51cto.com/964211/282387转载   UBOOT的版本更新速度比较快,截止今天,稳定正式的版本是u-boot-2009.11-rc2 ...

  10. ArcGIS 10.3 for Desktop新特性介绍

    ArcGIS 10.3是一个完整公布的ArcGIS平台,它包含新的产品(ArcGIS Pro),针对10.2版本号产品进行了功能增强和稳定性的改进. ArcGIS 10.3 for Server新特性 ...