117. Populating Next Right Pointers in Each Node II (Tree; WFS)
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)的更多相关文章
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 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 ...
- 【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 ...
- 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 ...
- 117. Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 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 ...
- [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 ...
随机推荐
- cousera 吴恩达 深度学习 第一课 第二周 作业 过拟合的表现
上图是课上的编程作业运行10000次迭代后,输出每一百次迭代 训练准确度和测试准确度的走势图,可以看到在600代左右测试准确度为最大的,74%左右, 然后掉到70%左右,再掉到68%左右,然后升到70 ...
- Oracle SEQUENCE 具体说明
ORACLE SEQUENCE ORACLE没有自增数据类型,如需生成业务无关的主键列或惟一约束列,能够用sequence序列实现. CREATE SEQUENCE语句及參数介绍: 创建序 ...
- CCNode的属性说明
class CC_DLL CCNode : public CCObject { protected://属性列表 float m_fRotationX; ///x轴旋转角度 float m_fRota ...
- Spring Could 问题
作为流行的微服务框架,Spring Could实用但不完美,比如说它只针对Java语言,坚持REST协议做微服务间的通讯等. Spring Cloud虽然集成了众多组件,可以构建一个完整的微服务应用, ...
- 中兴 F412 超级帐号telecomadmin破解(适用2015版h啊RowCount="0") TEWA-300AI EPON TEWA-500AI EPON破解
1.telnet 192.168.1.1 root/Zte521 有些密码也是root 2.输入sendcmd 1 DB p UserInfo 老本大多数教程会返回超级管理员帐号密码: < ...
- 【monkeyrunner】monkeyrunner脚本录制和回放
脚本录制 1.连接你已经打开调试模式的ANDROID设备或模拟器,输入adb devices 2.运行录制脚本.在cmd窗口输入 monkeyrunner recorder.py #recorder. ...
- SpringIoc 和 工厂模式(反射实现)
一.先演示 “简单工厂”: package org; interface Fruit { public void eat(); } class Apple implements Fruit { pub ...
- LCD的接口类型详解
LCD的接口有多种,分类很细.主要看LCD的驱动方式和控制方式,目前手机上的彩色LCD的连接方式一般有这么几种:MCU模式,RGB模式,SPI模式,VSYNC模式,MDDI模式,DSI模式.MCU模式 ...
- MariaDB Galera Cluster的配置测试
参考的https://fykuan.hsnuer.net/blog/2015/01/23/debian-%E4%B8%8A%E5%AE%89%E8%A3%9D-mariadb-galera-clust ...
- 黄聪:VPS服务器如何配置PHP.ini解决wordpress使用WP-Mail-SMTP插件发邮件出现Could not connect to SMTP host的解决办法
1.首先是WP-Mail-SMTP的下载地址:http://wordpress.org/plugins/wp-mail-smtp/ 2.出现Could not connect to SMTP host ...