• Populating Next Right Pointers in Each Node II ?


     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     }

    第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

     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     }

    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,#}
  • 相关阅读:
    Ubuntu16.04安装Docker、nvidia-docker
    Java 对象和类
    Java基础知识总结
    Java 学习路线
    编写radware的负载配置
    MySQL的主从复制+双主模式
    模拟MBR Grub故障修复
    搭建Nginx(haproxy)+keepalived+Tomcat双主高可用负载均衡
    golang数据库操作初体验
    我操蛋的2019
  • 原文地址:https://www.cnblogs.com/crane-practice/p/3617330.html
Copyright © 2020-2023  润新知