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. 在js中,ajax放在for中,ajax获取得到的变量有误

    先看代码 for(var i=0;i<tds.length;i++){ mui.ajax(url+'api/client/gifts/isSigned', {data :{ sqId:" ...

  2. 使用Monkey对apk做稳定性测试

    认识Monkey 官方文档:https://developer.android.com/studio/test/monkey.html 什么是Monkey? Monkey是Android中的一个命令行 ...

  3. AFNetworking3.1 基本使用

    #import "HttpsManager.h" @implementation HttpsManager #pragma mark - 创建请求者 +(AFHTTPSession ...

  4. react use simditor

    1.install simditor 2.import simditor && scss import $ from "jquery" import Simdito ...

  5. Android application testing with the Android test framework

    目录(?)[-] Android automated testing 1 How to test Android applications Tip 2 Unit tests vs functional ...

  6. js 扩展实例

    //扩展实例1 字符串中首字符转大写 var test=' this is '; String.prototype.mytrim=function(){ var re=/^\s+(.*?)\s+$/; ...

  7. phpcms URL修改

    修改caches\configs\system.php中'html_root' => 'html',//生成静态文件路径改成'html_root' => '',//生成静态文件路径,然后修 ...

  8. 高级C/C++编译技术之读书笔记(一)之编译/链接

    最近有幸阅读了<高级C/C++编译技术>深受启发,该书深入浅出地讲解了构建过程(编译.链接)中的各种细节,从多个角度展示了程序与库文件或代码的集成方法,提出了面向代码复用和系统集成的软件架 ...

  9. python3 chromeDriver 安装与配置

    1. 准备工作 在这之前请确保已经正确安装好了Chrome浏览器并可以正常运行,安装过程不再赘述. 2. 查看版本 点击Chrome菜单"帮助"→"关于Google Ch ...

  10. Servlet中的Filter(过滤器)

     Filter,过滤器,是处于客户端与服务器资源文件之间的一道过滤网,在访问资源文件之前,通过一系列的过滤器对请求进行修改.判断等,把不符合规则的请求在中途拦截或修改.也可以对响应进行过滤,拦截或修改 ...