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. C# 读 xml注释

    有这么个需求: 要统计所有的配置文件,这些配置文件都xml格式,并把这些配置写到数据表里,如果有注释要把这些注释写到对应配置对象的描述字段上 <item id="" key= ...

  2. js中加入数据缓存

    因为我们的系统设计 所有的数据查询全部是采用参数化json 后台解析后进行数据返回 由于使用统一的数据查询入口 所有可以很方便的为数据设置缓存 var ModelDataCache = new Arr ...

  3. 计算机原理--cpu篇

    简介 本文的目的是为了能够对特定的计算模型估算所需的CPU规格,个数. 这里主要介绍CPU的基本工作原理,指令集.(仅以X86体系结构的CPU为例 )

  4. unity, change shader for me and all my children

    public void changeShaderForMeAndAllMyChildren(){ Transform[] childrenTransformList = GetComponentsIn ...

  5. 点滴积累【C#】---C#实现上传word将路径保存到数据库,文件保存到服务器。并且按照名称读取服务器的word

    效果: 1. . . 数据库: 思路: 上传:先获取word物理地址,然后根据文件的类型判断,然后再保存到相应的文件夹下,再把路径插入到数据库中. 读取:首先根据输入的文件名字在数据库中查找出来文件的 ...

  6. Openresty + nginx-upload-module支持文件上传

    0. 说明 这种方式其实复杂,麻烦!建议通过这个方式搭建Openresty文件上传和下载服务器:http://www.cnblogs.com/lujiango/p/9056680.html 1. 包下 ...

  7. Atitit.故障排除系列---NoClassDefFoundError  NoClassDefFoundError ClassNotFoundException

    Atitit.故障排除系列---NoClassDefFoundError  NoClassDefFoundError ClassNotFoundException 1. java.lang.Class ...

  8. 亚信UED前端流程自动化构建工具

    亚信UED前端流程自动化构建工具 .wmd-input, .wmd-input:focus, #md-section-helper {font-size: 14px !important;line-h ...

  9. Linux中如何设置服务自启动?

    转自:Linux中如何设置服务自启动? 有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务,主要用三种方式进行这一操作: ln -s             在/etc/rc.d/rc ...

  10. PHP标签

    1.PHP原标签 2.脚本标签 3.短标签 需要short_open_tag=on,默认情况下为on 4.asp标签 需要asp_tags为on,默认为off