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 ...
随机推荐
- 三十七 Python分布式爬虫打造搜索引擎Scrapy精讲—将bloomfilter(布隆过滤器)集成到scrapy-redis中
Python分布式爬虫打造搜索引擎Scrapy精讲—将bloomfilter(布隆过滤器)集成到scrapy-redis中,判断URL是否重复 布隆过滤器(Bloom Filter)详解 基本概念 如 ...
- Pro C/C++ 编程中值得注意的问题
1.宿主字符串存储Oracle自动补零问题. EXEC SQL BEGIN DECLARE SECTION; unsigned char liId[25]; EXEC SQL END DECLARE ...
- WAF 强化学习
参考:https://github.com/duoergun0729/3book/tree/master/code/gym-waf 代码: wafEnv.py #-*- coding:utf-8 –* ...
- 【spark】共享变量
Spark中的两个重要抽象是RDD和共享变量. 一般情况下,当Spark在集群的多个不同节点的多个任务上并行运行一个函数的时候, 它会把函数中涉及到的每个变量在每个节点每个任务上都生成一个副本. Sp ...
- maven打的包中含源文件jar包
如何用maven package打的jar包中还包含源文件(resource)jar包: 在pom中如下添加: <build> <plugins> <plugin> ...
- 剑指Offer(第二版)面试案例:树中两个节点的最低公共祖先节点
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74612786冷血之心的博客) 剑指Offer(第二版)面试案例:树 ...
- Arcgis for javascript不同的状态下自定义鼠标样式
俗话说:爱美之心,人皆有之.是的,没错,即使我只是一个做地图的,我也希望自己的地图看起来好看一点.在本文,给大家讲讲在Arcgis for javascript下如何自定义鼠标样式. 首先,说几个状态 ...
- Creating a Game with CocosBuilder
Creating a Game with CocosBuilder This tutorial aims to show how you can use CocosBuilder together w ...
- Linux 下新增用户的流程
一: 新增用户 1.添加新的用户账号使用useradd命令,其语法如下: 代码:useradd 选项 用户名其中各选项含义如下: 代码:-c comment 指定一段注释性描述.-d 目录 指定用户主 ...
- CodeForces - 963D:Frequency of String (bitset暴力搞)
You are given a string ss. You should answer nn queries. The ii-th query consists of integer kiki an ...