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. 快速建立ssh互信(转)

    转自:魏巍的Linux酒吧 - 51CTO技术博客(http://weiweilinux.blog.51cto.com/3349074/1048212) 快速建立ssh互信 因为工作中经常需要配置服务 ...

  2. 【spring boot logback】日志使用自定义的logback-spring.xml文件后,application.properties中关于日志的相关配置还会起作用么

    本篇 将针对[日志使用自定义的logback-spring.xml文件后,application.properties中关于日志的相关配置还会起作用么]这一个主题进行探索. 这个测试项目是根据[spr ...

  3. [置顶] pycurl检测网站性能,pycurl.*_TIME时间问题

    今天使用python+pycurl来检测网站性能,使用curl_obj.getinfo(pycurl.*_TIME)来获取各个阶段运行时间 total_time = curl_obj.getinfo( ...

  4. Linux下ntp时间同步

    在root用户下执行 先安装同步时间软件,每台机器执行 yum install -y ntp 然后执行以下命令: crontab -e */10 * * * * /usr/sbin/ntpdate - ...

  5. 2017.2.28 activiti实战--第五章--用户与组及部署管理(二)部署流程资源

    学习资料:<Activiti实战> 第五章 用户与组及部署管理(二)部署流程资源 内容概览:讲解流程资源的读取与部署. 5.2 部署流程资源 5.2.1 流程资源 流程资源常用的有以下几种 ...

  6. UE把环境变量Path改了

    为了比较个文件,装了UE. 文件比较完了,环境变量也被改了. 改还不是写添加式的改,是写覆盖式的改. 搞得ant都起不动了,一看Path被改的那样(C:\hy\soft\ultraedit\Ultra ...

  7. vuex 中关于 mapGetters 的作用

    mapGetters 工具函数会将 store 中的 getter 映射到局部计算属性中.它的功能和 mapState 非常类似,我们来直接看它的实现: export function mapGett ...

  8. Git 学习之--安装配置GitHub

    楼主今天学习了一下Git的使用,而且Androdi studio 下加入了Git插件,成功提交项目到自己Github个人主页 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...

  9. 连续调用scanf的问题总结

    对于非常简单的scanf函数,一直使用,但是却是有很多的知识点没有掌握好,现总结如下: 1.多个scanf之后,后序以 scanf("%c",&c) 当程序连续调用scan ...

  10. ubuntu下编译原生ffmpeg

    本文主要介绍Linux 系统下如何编译Ffmpeg,编译环境是Ubuntu 16.04,Ffmpeg版本是3.4.2.Windows环境 下如何编译ffmpeg前面有博文介绍,也录有视频,感兴趣的同学 ...