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 ...
随机推荐
- ZetCode PyQt4 tutorial Drag and Drop
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This is a simple ...
- 【转载】python安装numpy和pandas
转载:原文地址 http://www.cnblogs.com/lxmhhy/p/6029465.html 最近要对一系列数据做同比比较,需要用到numpy和pandas来计算,不过使用python安装 ...
- 458 - The Decoder & C语言gets函数,字符输出输出 & toascii()
Write a complete program that will correctly decode a set of characters into a valid message. Your p ...
- opencrud graphql 数据操作指南
opencrud 是社区团队提出,同时prisma框架就是按照这个标准设计的,里面包含了对于graphql 数据 操作的最佳实践,目前还在完善中,但是设计以及指南覆盖的功能还是比较全的,如果用过 pr ...
- centeros php 实战
apache 默认安装路径 Fedora Core, CentOS, RHEL:ServerRoot :: /etc/httpdPrimary Config Fle ...
- Redis 集群方案介绍
由于Redis出众的性能,其在众多的移动互联网企业中得到广泛的应用.Redis在3.0版本前只支持单实例模式,虽然现在的服务器内存可以到100GB.200GB的规模,但是单实例模式限制了Redis没法 ...
- 7z 7zip 日期、时间,文件名
from: http://hi.baidu.com/guicomeon/item/c0957c373972fbc52f8ec26e 先说明一点,要注意区分当前所使用的系统,中文系统和英文系统是有区别的 ...
- mysql-4连接
联合多表查询 菜鸟教程join 日常应用较多的是从多个表格中获取数据.使用join可以在多个表查询进行select.update.delete. join按照功能分为三类: inner join(内连 ...
- nohup 后台运行命令
在Linux上部署zipkin,在SSH客户端执行java -jar zipkin-server-1.21.0-exec.jar,启动成功,在关闭SSH客户端后,运行的程序也同时终止了,怎样才能保证在 ...
- 双系统(Windows+Ubuntu)重装Ubuntu后,修复引导
重装Ubuntu后,重启机器,Windows系统消失了.进入BIOS修改启动顺序,Windows系统恢复了,但是,刚装好的Ubuntu没了,一开机,电脑自动进入Windows. 解决办法: 用装系统的 ...