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:
/**
* 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) {
if (root==null)
return; root.next = null;
TreeLinkNode levelHead = root; while (true){
boolean hasChild = false;
TreeLinkNode cur = levelHead;
TreeLinkNode nextLevelPre = null;
TreeLinkNode nextLevelHead = null;
while (cur!=null){
if (cur.left!=null){
hasChild = true;
if (nextLevelPre!=null){
nextLevelPre.next = cur.left;
nextLevelPre = cur.left;
} else {
nextLevelPre = cur.left;
nextLevelHead = cur.left;
}
} if (cur.right!=null){
hasChild = true;
if (nextLevelPre!=null){
nextLevelPre.next = cur.right;
nextLevelPre = cur.right;
} else {
nextLevelPre = cur.right;
nextLevelHead = cur.right;
}
} cur = cur.next;
}
if (hasChild)
nextLevelPre.next = null; //If reach the last level, then stop.
if (!hasChild)
break; //Move to next level.
levelHead = nextLevelHead;
} return;
}
}

At each level, after we construct the link list, we have a linked list to visit all nodes in this level. Then we can visit all child nodes in the next level along this linked list.This is a very smart way.

Note: For this question, we need to be careful about where the first child node in the next level is.

Leetcode-Populating Next Right Pointer in Binary Tree II的更多相关文章

  1. [Leetcode] Populating next right pointer in each node ii 填充每个节点的右指针

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

  2. LeetCode: Populating Next Right Pointer in Each Node

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

  3. 【一天一道LeetCode】#104. Maximum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  4. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  5. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  6. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

  7. LeetCode Verify Preorder Serialization of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ 题目: One way to ...

  8. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  9. LeetCode之104. Maximum Depth of Binary Tree

    -------------------------------- 递归遍历即可 AC代码: /** * Definition for a binary tree node. * public clas ...

随机推荐

  1. 解决nginx到后端服务器Connection: close问题

    以下是官方原文: By default, NGINX redefines two header fields in proxied requests, “Host” and “Connection”, ...

  2. 点滴积累【SQL Server】---使用Kettle实时同步DB2数据到SQLserver

    效果: 描述: 此操作适用于单点登录的同步用户. 首先,使用kettle将DB2数据同步到SQL中,然后添加到windows的任务计划中.定时执行同步数据. 特殊说明:此工具涉及到公司版权,所以不方便 ...

  3. C#.NET中使用BackgroundWorker在模态对话框中显示进度条

    这里是一个示例,其中展示了如何使用Backgroundworker对象在模态对话框中显示后台操作的实时进度条. 首先是主窗体代码: using System; using System.Collect ...

  4. InnoDB:表

    数据在表中是如何进行组织存放的?下面我们就来看看: InnoDB引擎表的类型 InnoDB表都会有一个主键. 如果没有显示的指定主键,首先会去查找,看是否有非空的唯一索引, 如果有,则该列为主键:如果 ...

  5. mysql之mysql、mysql-devel、mysql-server

    一.引言 一直都没搞明白这三者之间的关系,于是决心查资料把这个问题搞明白,遂记录以便查阅.学习 二.关系 mysql-devel 开发用到的库以及包含文件mysql mysql 客户端mysql-se ...

  6. ORACLE建立物化视图

    --使用 on commit 的方式建立物化视图 create materialized view emp_dept refresh on commit as select t.*,d.dname f ...

  7. ZThread在Windows下打印若干DEBUG信息到console的原因

    代码见这篇随笔 在Windows下的运行结果:ZThread打印了一堆东西(并不是我写的) 文件结构: 最开始我以为是编译选项没弄对,同样的代码放到Linux下编译,还是打印这些信息 注意我在Linu ...

  8. 逻辑漏洞-客户端验证的邮箱-Web渗透实例之中国教育部青少年普法网站逻辑漏洞

    转载自:http://www.zmnhssn.com/?post=61 漏洞地址:https://user.qspfw.com  用户登陆界面 具体漏洞地址:    用户密码找回界面:https:// ...

  9. spring-pool.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  10. Linux上安装Nginx及常用命令

    一.Linux安装软件常用方法 1.rpm(或pkg)安装,类似于Windows安装程序,是预编译好的程序. 1)使用的是通用参数编译,配置参数不是最佳 2)可控制性不强,比如对程序特定组件的定制性安 ...