Question

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

Solution

思路与Populating Next Right Pointer 一样,仍是用四个指针 prevHead, prevCurrent, curHead, current层次遍历。

两层循环:

外层循环:traverse level by level

内层循环: traverse last level, link current level

 /**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void connect(TreeLinkNode root) {
TreeLinkNode prevHead = root, prevCur = root;
TreeLinkNode currentHead = null, current = null;
while (prevHead != null) {
prevCur = prevHead;
// Find current head
while (prevCur != null && prevCur.left == null && prevCur.right == null) {
prevCur = prevCur.next;
}
if (prevCur == null) {
break;
} else if (prevCur.left != null) {
currentHead = prevCur.left;
current = currentHead;
if (prevCur.right != null) {
current.next = prevCur.right;
current = current.next;
}
} else {
currentHead = prevCur.right;
current = currentHead;
}
// link current level
prevCur = prevCur.next;
while (prevCur != null) {
if (prevCur.left != null) {
current.next = prevCur.left;
current = current.next;
}
if (prevCur.right != null) {
current.next = prevCur.right;
current = current.next;
}
prevCur = prevCur.next;
}
// reset
prevHead = currentHead;
} }
}

Populating Next Right Pointers in Each Node II 解答的更多相关文章

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

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

  3. 29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node OJ: https://oj.leetcode.com/problems/populating-next-rig ...

  4. Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...

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

  6. Leetcode 树 Populating Next Right Pointers in Each Node II

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...

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

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

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

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

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

随机推荐

  1. RequireJS入门(一)

    RequireJS由James Burke创建,他也是AMD规范的创始人. RequireJS会让你以不同于往常的方式去写JavaScript.你将不再使用script标签在HTML中引入JS文件,以 ...

  2. zoj 3706 Break Standard Weight(dp)

    Break Standard Weight Time Limit: 2 Seconds                                     Memory Limit: 65536 ...

  3. Android 如何检测一个服务是否还在运行?

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  4. 【ArcGIS 10.2新特性】ArcGIS Online新特性(上)

    概述 7月,ArcGIS Online发布了很多更新.主要内容有:新的网站设计,增强了Web制图,数据分析,应用程序创建,以及机构管理等功能. 更新的大致内容总结如下: 地图查看器:新的分析工具.获取 ...

  5. ibatis使用--SqlMapClient对象

    SqlMapClient对象 这个对象是iBatis操作数据库的接口(执行CRUD等操作),它也可以执行事务管理等操作.这个类是我们使用iBATIS的最主要的类.它是线程安全的.通常,将它定义为单例. ...

  6. React 入门最好的实例-TodoList

    React 的核心思想是:封装组件,各个组件维护自己的状态和 UI,当状态变更,自动重新渲染整个组件. 最近前端界闹的沸沸扬扬的技术当属react了,加上项目需要等等原因,自己也决定花些时间来好好认识 ...

  7. C# winform带进度条的图片下载

    代码如下: public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } private void ...

  8. 注意:"AspNetPager”的控件“AspNetPager1”必须放在具有 runat=server 的窗体标记内

    应加: <form id="form1" runat="server"> </form> 否则一开始什么也不显示,页面控件看不见,加上a ...

  9. Linux设置日期

    $ date -s "2016-07-13 14:54" 把时间设置为2016-07-13 14:54

  10. 高级子查询【weber出品必属精品】

    多列子查询 where条件中出现多列与子查询进行比较 多列子查询分为:成对比较和非成对比较 成对比较: SQL> select ename,sal,job from emp where (dep ...