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 ?的更多相关文章

  1. 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 ...

  2. 【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 ...

  3. 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 ...

  4. 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 ...

  5. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  6. Leetcode 树 Populating Next Right Pointers in Each Node II

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...

  7. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

    Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...

  8. 【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 ...

  9. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

随机推荐

  1. yii2:frontend/frontactoin curl生成

    yii2:frontend/frontactoin curl生成 想要覆写已存在文件,选中 “overwrite” 下的复选框然后点击 “Generator”.如果是新文件,只点击 “Generato ...

  2. Appium 自动化测试(5)-- Appium详细介绍:Appium 手机自动化测试_TesterHome公开版pdf

  3. 【scala】可变参数

    Scala允许使用可变的参数列表. 语法 在声明的参数类型后边添加星号(*) 示例 object HelloWorld{ def hello(args:String*): Unit ={ for(ar ...

  4. 关于微软C#中的CHART图表控件的简单使用

    最近公司项目要用到Chart图表控件,这是一个比较老的东西了,目前网络上似乎已经不太流行这个控件,但是只要配置了相关的属性,效果还是可以的.前前后后摸索了好久,接下来谈谈这个件控件最重要的几个属性. ...

  5. Elasticsearch 文档专用

    ES安装等操作 http://blog.csdn.net/cnweike/article/details/33736429 https://www.elastic.co/guide/cn/elasti ...

  6. 关于htonl()

    htons #include <arpa/inet.h> uint16_t htons(uint16_t hostshort); htons的功能:                     ...

  7. 【SQL查询】视图_view

    转自:http://database.e800.com.cn/articles/2009/719/1248015564465_1.html 视图是从一个或几个基本表(或视图)导出的表.它与基本表不同, ...

  8. elasticsearch聚合案例--分组、求最大值再求最大值的均值

    一.需求 A.B.C代表3个用户,第二列代表各自的得分,求A.B.C的最好成绩以及A.B.C最好成绩的均值 A 10 A 11 A 13 B 11 B 11 B 12 C 10 C 10 C 11 C ...

  9. Win7系统64位环境下使用Apache——Apache2.4整合Tomcat与mod_jk

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/70398091 本文出自[我是干勾鱼的博客] 之前的几篇文章: Win7系统64位 ...

  10. 该文档举例说明了multimap的查找和删除元素的使用

    该文档举例说明了multimap的查找和删除元素的使用. 其中,在使用迭代器遍历元素的时候,如果使用了删除迭代器的操作,那么需要小心迭代器失效的情况. /* 功能说明: multimap的查找和删除元 ...