首页 > 其他 > 详细

Populating Next Right Pointers in Each Node II ?

时间:2014-03-22 16:22:44      阅读:445      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 1     void connect(TreeLinkNode *root) {
 2         if(root==NULL)
 3             return;
 4         if(root->left&&root->right)
 5             root->left->next=root->right;
 6         if(root->next){
 7             if(root->left!=NULL&&root->right==NULL){
 8                 if(root->next->left)
 9                     root->left->next=root->next->left;
10                 else if(root->next->right)
11                     root->left->next=root->next->right;
12             }
13             else if(root->left==NULL&&root->right!=NULL){
14                 if(root->next->left)
15                     root->right->next=root->next->left;
16                 else if(root->next->right)
17                     root->right->next=root->next->right;
18             }
19             else if(root->left!=NULL&&root->right!=NULL){//这个也不能少哇
20                 if(root->next->left)
21                     root->right->next=root->next->left;
22                 else if(root->next->right)
23                     root->right->next=root->next->right;
24             }
25             else if(root->left==NULL&&root->right==NULL){//呃,这个也不能少,少了,被第33个测试用例给检查出来了,呃,不会了
26                 
27             }
28         }
29         connect(root->left);
30         connect(root->right);
31     }
bubuko.com,布布扣

第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

bubuko.com,布布扣
 1     void connect(TreeLinkNode *root) {
 2         if(root==NULL)
 3             return;
 4         TreeLinkNode *rootNext,*next;//一个是root的next,一个是子节点要连的next
 5         rootNext=root->next;
 6         next=NULL;
 7         //寻找当前子节点要连的next
 8         while(rootNext){
 9             if(rootNext->left){
10                 next=rootNext->left;
11                 break;
12             }
13             else if(rootNext->right){
14                 next=rootNext->right;
15                 break;
16             }
17             else{
18                 rootNext=rootNext->next;
19             }
20         }
21         if(root->left&&root->right) //攘外必先安内
22             root->left->next=root->right;
23         if(next){
24             if(root->right)
25                 root->right->next=next;
26             else if(root->left)
27                 root->left->next=next;
28         }
29         //connect(root->left);
30         connect(root->right);
31         connect(root->left);
32     }
bubuko.com,布布扣

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 ?,布布扣,bubuko.com

Populating Next Right Pointers in Each Node II ?

原文:http://www.cnblogs.com/crane-practice/p/3617330.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!