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 ...
随机推荐
- 【scala】定义变量和函数
1.定义变量 Scala的变量分为两种,val和var. val跟java的final变量类似,一旦初始化就不能被重新赋值. var类似于java的非final变量,在整个生命周期内var可以被重新赋 ...
- Mongodb 补充
1 mongodb 概述 启动mongo 1 数据库操作 没有数据的 集合 和 数据库不会显示 db 查看当前的数据库名称: 所有物理上存在的数据库 db.stats() 查看当前的数据库信息: sh ...
- B. Clique Problem(贪心)
题目链接: B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- mac brew安装使用卸载
(一)安装 1.浏览器打开brew.sh,进入homebrew主页.找到install homebrew 的命令: /usr/bin/ruby -e "$(curl -fsSL https: ...
- linux进程通信全面解析
进程IPC 的 7种方式 linux下 进程通讯IPC的方式主要有以下7种: 1.文件 2.共享内存 3.信号 4.管道 5.套接字 6.消息列队 7.信号量 以下正文 中 一一 分析下: 1 ...
- vue中使用axios发送请求
我们知道,vue2.0以后,vue就不再对vue-resource进行更新,而是推荐axios,而大型项目都会使用 Vuex 来管理数据,所以这篇博客将结合两者来发送请求 1.安装axios cnpm ...
- linux 系统监控某目录下文件及文件夹的变化
inotifywait 是一个可以实时监控文件变动的工具,它利用linux内核中的inotify机制实现监控功能. 查看内核版本 [root@Oracle ~]# uname -r 2.6.32-22 ...
- 【ES6】箭头函数
let getPrices = () => 4.55 console.log(getPrices()) let arr = ['apple', 'banana', 'orange'] arr.f ...
- POJ1797 Heavy Transportation
解题思路:典型的Kruskal,不能用floyed(会超时),上代码: #include<cstdio> #include<cstring> #include<algor ...
- android 进制转换方法
import android.util.Log; public class CommandHelper { public static String intToHexString(int value) ...