/压哨提交的第三题,不知道提交上去没有。。。前几次有点问题,前序遍历+动态规划 class LongestPath { public:     int findPath(TreeNode* root) {         vector<int> white(301);         vector<int> black(301);         stack<TreeNode> number;         int max=0;         int count=0;         if(!root)             return 0;         if(root->val==0)            {             white[count]=1;             black[count]=0;             max=white[count];             count++;            }          else            {             black[count]=1;             white[count]=0;             max=black[count];             count++;            }         TreeNode* p=root;                  while(p||!number.empty())         {           if(p==root)           {               number.push(*p);               p=p->left;               continue;           }           if(p)           {             number.push(*p);             if(p->val==0)               {                white[count]=white[count-1]+1;                black[count]=0;                if(max<white[count])                  max=white[count];                count++;                               }             if(p->val==1)               {                black[count]=black[count-1]+1;                white[count]=0;                if(max<black[count])                  max=black[count];                count++;               }              p=p->left;           }                      else           {             TreeNode temp =number.top();             p=temp.right;             if(p)              continue;             else              {                number.pop();                count--;              }                        }                    }         return max;                  } };
点赞 2

相关推荐

不愿透露姓名的神秘牛友
06-12 18:53
第一次听说还有无水工作!!!又是被刷新三观的一天
Lynn012:666第一次听到,你给他说这里不方便我们加个微信
点赞 评论 收藏
分享
仁者伍敌:难怪小公司那么挑剔,让你们这些大佬把位置拿了
点赞 评论 收藏
分享
牛客773130651号:巨佬,简历模板换成上下的,左右的很烦,hr看着不爽。。。科大随便乱杀,建议能保研就保研,不行也得考一下 ,985硕去干算法,比开发强多了。开发许多双非都能搞,学历优势用不上,算法有门槛
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务