/压哨提交的第三题,不知道提交上去没有。。。前几次有点问题,前序遍历+动态规划 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

相关推荐

点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务