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
class Solution {
public:
void connect(TreeLinkNode *root) {
TreeLinkNode* parent;
TreeLinkNode* current;
TreeLinkNode* nextParent = root; while(){
parent = nextParent;
nextParent = NULL;
while(parent){ //find nextParent
if(parent->left) {
nextParent = parent->left;
break;
}
if(parent->right){
nextParent = parent->right;
break;
}
parent = parent->next;
}
current = nextParent;
if(!current) return; while(parent){
if(current == parent->left){//add next pointer of left child
if(parent->right) current->next = parent->right;
else{ //find next until parent equals null
parent = parent->next;
while(parent){
if(parent->left) {
current->next = parent->left;
break;
}
if(parent->right){
current->next = parent->right;
break;
}
parent = parent->next;
}
}
}
else{ //add next pointer of right child
parent = parent->next;
while(parent){ //find next until parent equals null
if(parent->left) {
current->next = parent->left;
break;
}
if(parent->right){
current->next = parent->right;
break;
}
parent = parent->next;
}
}
current = current->next;
} // end of level
}
}
};

117. Populating Next Right Pointers in Each Node II (Tree; WFS)的更多相关文章

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

  2. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

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

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

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

  6. 117. Populating Next Right Pointers in Each Node II

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

  7. 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II

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

  8. Java for LeetCode 117 Populating Next Right Pointers in Each Node II

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

  9. [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 ...

随机推荐

  1. Ubuntu 16.04系统开机紫屏的解决办法

    具体症状为卡在开机界面,按任何键都无反应. 网上查看了几篇文章 ,如下: 解决:ubuntu16.04启动时长时间停留在紫屏或跳文本的黑屏界面 Ubuntu16.04显卡驱动 电源管理 里面提到的开机 ...

  2. Alpha冲刺(11/10)

    拖鞋旅游队团队事后诸葛亮会议 前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10054510.html 时间:2018-12-1 20:00 地 ...

  3. L188

    This is the view from the instrument deployment camera of InSight, America’s latest probe to Mars, w ...

  4. myeclipse web servelet调试输入的中文在TOMCAT服务器的命令行显示为????

    B 问题:myeclipse web servelet调试输入的中文在TOMCAT服务器的命令行显示为???? 解决:调整JSP页面编码:gb2312--->utf-8

  5. Centos 中扩展 软件源 的安装 之 Remi ( 为yum 扩展软件源 )

    平时一般都是使用Ubuntu的,最近用起来Centos 发现软件安装方便不是很方便,   在安装过程中接触到了这么一个概念,  就是为yum 安装 扩展源,  这里下面要说的就是其中的  Remi  ...

  6. Codeforces 1009D:Relatively Prime Graph

    D. Relatively Prime Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. DesignPattern(五)行为型模式(上)

    行为型模式 行为型模式是对在不同对象之间划分责任和算法的抽象化.行为模式不仅仅关于类和对象,还关于它们之间的相互作用.行为型模式又分为类的行为模式和对象的行为模式两种. 类的行为模式——使用继承关系在 ...

  8. 使用nomad && consul && fabio 创建简单的微服务系统

    具体每个组件的功能就不详细说明了 nomad 一个调度工具,consul 一个服务发现,健康检查多数据中心支持的工具 fabio 一个基于consul的负载均衡&&动态路由工具,对于集 ...

  9. 每天进步一点点——Linux中的线程局部存储(一)

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/26469435    在Linux系统中使用C/C++进行多线程编程时,我们遇到最多的就是对同 ...

  10. 安全关闭MySQL

    想要安全关闭 mysqld 服务进程,建议按照下面的步骤来进行: 0.用具有SUPER.ALL等最高权限的账号连接MySQL,最好是用 unix socket 方式连接: 1.在5.0及以上版本,设置 ...