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. 一些制作app的软件

    搜狐快站 http://www.kuaizhan.com/百度siteapp http://siteapp.baidu.com/腾讯风铃 http://fl.qq.com/

  2. SQL面试题: 数据库中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列 ,当B列大于C列时选择B列否则选择C列 ,

    1.用一条sql语句 select (case when a>b then a else b end ),(case when b>c then b esle c end)  from 表 ...

  3. 中小型研发团队架构实践:任务调度Job

    一.Job 简介 Job 类似于数据库中的作业,多用于实现定时执行任务.适用场景主要包括定时轮询数据库同步.定时处理数据.定时邮件通知等. 我们的 Job 分为操作系统级别定时任务 WinJob 和 ...

  4. NOJ 1012 进制转换(十进制转换成随意进制)

    题目: 进制转换 时间限制(普通/Java) : 1000 MS/ 3000 MS          执行内存限制 : 65536 KByte总提交 : 1819            測试通过 : ...

  5. Microsoft SQL Server Data Tools - Business Intelligence for Visual Studio 2013 SSIS

    VS2012 SSDTBI_VS2012_x86_CHS.exe http://www.microsoft.com/zh-CN/download/details.aspx?id=36843 VS201 ...

  6. 解决WinForm界面闪烁问题

    前言 之前將.net 1.1 Windows Form程式升級到.net 4.0,結果在開畫面時,閃的非常利害!  於是就開始找解決方法. 研究及解決 開始找到了設定DoubleBuffer=true ...

  7. app store上传图片显示错误:未能创建 屏幕快照

    在iTunes Connect中加入一个app后.加入屏幕快照时,依照要求的尺寸上传照片成功,可是在保存的时候提示"未能创建Screenshots for 4-inch iPhone5 an ...

  8. position固定菜单栏

    position:fixed来实现. <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  9. POJ--3529--Wormholes

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36889   Accepted: 13520 Descr ...

  10. MySQL主从同步那点事儿

    一.前言 关于mysql主从同步,相信大家都不陌生,随着系统应用访问量逐渐增大,单台数据库读写访问压力也随之增大,当读写访问达到一定瓶颈时,将数据库的读写效率骤然下降,甚至不可用;为了解决此类问题,通 ...