Populating Next Right Pointers in Each Node II ?
void connect(TreeLinkNode *root) {
if(root==NULL)
return;
if(root->left&&root->right)
root->left->next=root->right;
if(root->next){
if(root->left!=NULL&&root->right==NULL){
if(root->next->left)
root->left->next=root->next->left;
else if(root->next->right)
root->left->next=root->next->right;
}
else if(root->left==NULL&&root->right!=NULL){
if(root->next->left)
root->right->next=root->next->left;
else if(root->next->right)
root->right->next=root->next->right;
}
else if(root->left!=NULL&&root->right!=NULL){//这个也不能少哇
if(root->next->left)
root->right->next=root->next->left;
else if(root->next->right)
root->right->next=root->next->right;
}
else if(root->left==NULL&&root->right==NULL){//呃,这个也不能少,少了,被第33个测试用例给检查出来了,呃,不会了 }
}
connect(root->left);
connect(root->right);
}
第33个测试用例:Input:{1,2,3,4,5,#,6,7,#,#,#,#,8}Output:{1,#,2,3,#,4,5,6,#,7,#}Expected:{1,#,2,3,#,4,5,6,#,7,8,#}
参看:http://blog.csdn.net/fightforyourdream/article/details/16854731
void connect(TreeLinkNode *root) {
if(root==NULL)
return;
TreeLinkNode *rootNext,*next;//一个是root的next,一个是子节点要连的next
rootNext=root->next;
next=NULL;
//寻找当前子节点要连的next
while(rootNext){
if(rootNext->left){
next=rootNext->left;
break;
}
else if(rootNext->right){
next=rootNext->right;
break;
}
else{
rootNext=rootNext->next;
}
}
if(root->left&&root->right) //攘外必先安内
root->left->next=root->right;
if(next){
if(root->right)
root->right->next=next;
else if(root->left)
root->left->next=next;
}
//connect(root->left);
connect(root->right);
connect(root->left);
}
AC,可是为什么先连左后连右就会出错呀,递归啊,还是不明白呀????????????????????????!!!!!!
Status: Wrong Answer
Input: | {2,1,3,0,7,9,1,2,#,1,0,#,#,8,8,#,#,#,#,7} |
Output: | {2,#,1,3,#,0,7,9,1,#,2,1,0,#,7,#} |
Expected: | {2,#,1,3,#,0,7,9,1,#,2,1,0,8,8,#,7,#} |
Populating Next Right Pointers in Each Node II ?的更多相关文章
- 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】Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node OJ: https://oj.leetcode.com/problems/populating-next-rig ...
- Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...
- 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 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- 【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 Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
随机推荐
- Ajax技术基础
对于AJAX技术,感觉以前虽然用过但是对于AJAX没有一个清晰的认识,今天专门总结一下,希望掌握的更加牢固吧! 一,什么是AJAX? AJAX通常被叫做异步刷新技术,其实他也是可以同步的.主要都用于异 ...
- mysql 库与表操作
1. 库操作 1.1. 创建数据库 语法规则:create database 库名; CREATE DATABASE dt55; 在创建库时,希望指定编码语法:create database 库名 c ...
- MongoDB 可视化管理工具 MongoCola-1.1.0 测试版发布
首先,感谢大家对于本工具的支持. 经过一周的努力,最新版的工具测试版出炉了,这个版本是一个很重要的版本. 为什么说这个版本重要?以前的工具,只支持一个视图窗口,也就是说了,一次只能看一个数据集的数据. ...
- shell编程学习1
1.shell是操作系统的终端命令行 (1)shell可以理解为软件系统提供给用户操作的命令行界面,可以说它是人机交互的一种方式. (2)我们可以使用shell和操作系统.uboot等软件系统进 ...
- css11动态效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- ng $interval(周期性定时器) $timeout(延迟定时器)
<!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> <met ...
- wordpress 使用固定链接
官方文档 无插件移除url中category 目录前缀 设置 >> 固定链接,设置固定链接为自定义为: /%category%/%postname%/或者/%category%/%post ...
- POJ 2029 Palindromes _easy version
#include<cstdio> #include<cstring> using namespace std; int main() { int n; ]; scanf(&qu ...
- HDU - 6433: H. Pow (简答题,输出大数)
There are n numbers 3^0, 3^1, . . . , 3^n-1. Each time you can choose a subset of them (may be empty ...
- asp.net core microservices 架构之 分布式自动计算(二)
一 简介 上一篇介绍了zookeeper如何进行分布式协调,这次主要讲解quartz使用zookeeper进行分布式计算,因为上一篇只是讲解原理,而这次实际使用, ...