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 ...
随机推荐
- windows上安装maven及eclipse中配置maven
本地安装与配置: 1.jdk 在cmd中运行 Java -version 2.下载maven包 https://maven.apache.org/download.cgi下载最新版的Maven程序:( ...
- [置顶]
【机器学习PAI实践十一】机器学习PAI为你自动写歌词,妈妈再也不用担心我的freestyle了(提供数据、代码
背景 最近互联网上出现一个热词就是"freestyle",源于一个比拼rap的综艺节目.在节目中需要大量考验选手的freestyle能力,freestyle指的是rapper即兴的 ...
- Centos7 服务 service 设置命令 systemctl 用法 (替代service 和 chkconfig)
在Centos 中 systemctl 是设置系统服务的命令,即 service , 它融合之前service和chkconfig的功能于一体. 可以使用它永久性或只在当前会话中启用/禁用服务 ...
- jquery.cycle.js
jquery.cycle.js简单用法实例 样式: a{text-decoration: none;} *{margin:0; padding:0;} /*容器设置*/ .player { width ...
- Linux中文档去掉windows文本的多余的回车符(^M)
1) 使用sed 去掉windows下的回车符 (注意^M 在linux 下写法 按^M 是回车换行符,输入方法是按住CTRL+v,松开v,按m) sed -i 's/^M//g' filenam ...
- Gym 100712L Alternating Strings II(单调队列)
题目链接 Alternating Strings II 题意是指给出一个长度为n的01串,和一个整数k,要求将这个01串划分为很多子串(切很多刀),使得每个子串长度不超过k,且每个字串不是01交替出现 ...
- 使用rollup 开发专业js library
rollup 是一个不错的javascript 模块打包器,一般我们用来构建library 安装 npm install -g rollup 参考集成jquey && shortid ...
- Loararunner录制脚本
LoadRunner录制 1.启动LoadRunner,用管理员方式打开,选择 “Create/Edit Scripts” 2.从这两个方式任意方式打开脚本页面 3.选择协议,这里我们举例子,用“We ...
- bzoj2750最短路计数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2750 枚举每一个起点,通过该边的子树中有多少节点就知道本次它被经过几次了: 因为同一起点到该 ...
- bzoj3143游走
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3143 学到了无向图中点被经过的期望次数和边被经过的期望次数. 一个点被经过的期望次数 就是 ...