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. svn回滚版本2

    svn 版本回滚 取消对代码的修改分为两种情况:   第一种情况:改动没有被提交(commit). 这种情况下,使用svn revert就能取消之前的修改. svn revert用法如下: # svn ...

  2. zookeeer 集群和伪集群模式

    环境变量设置: # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # U ...

  3. 解决linux下javac -version和java -version版本显示不一致

    解决linux下javac -version和java -version版本显示不一致 [javascript] view plaincopy [root@localhost usr]# $JAVA_ ...

  4. C# - 委托_ 匿名方法

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  5. Goodle Clean设计架构

    Goodle Clean设计架构 23 * @param <P> the response type 24 */ 25 public abstract class UseCase<Q ...

  6. WCF技术剖析之四:基于IIS的WCF服务寄宿(Hosting)实现揭秘

    原文:WCF技术剖析之四:基于IIS的WCF服务寄宿(Hosting)实现揭秘 通过<再谈IIS与ASP.NET管道>的介绍,相信读者已经对IIS和ASP.NET的请求处理管道有了一个大致 ...

  7. Swift - 访问通讯录联系人(使用纯代码实现)

    1,通讯录访问介绍 通讯录(或叫地址簿,电话簿)是一个数据库,里面储存了联系人的相关信息.要实现访问通讯录有如下两种方式: (1)AddressBook.framework框架 : 没有界面,通过代码 ...

  8. android ble蓝牙开发略解

    Android 蓝牙4.0开发 1.  权限和相关属性 “android:required="true"表示apk只有在具有bluetooth_le属性的系统里运行,这个4.3之前 ...

  9. chrome你这是入侵OSX了么?

    今天突然发现安装chrome的应用后,会在Dock中出现如下图标,点击即可启动chrome相关应用,很是方便.

  10. thinkphp 设置跨域请求

    场景:我的本地网页服务器无法访问本地的接口服务器接口提示一下错误:大致意思是:是一个跨域请求我的没有访问该地址的权限(接口服务器采用的是PHP编写) XMLHttpRequest cannot loa ...