class LongestPath { public:     vector<int> findMaxPath(TreeNode* root, int &maxPath)         {         vector<int> tempMaxPath;//【0】储存最长的0色路径,【1】保存最长1色路径         tempMaxPath.push_back(0);         tempMaxPath.push_back(0);         if(!root)             return tempMaxPath;         vector<int> leftMaxPath = findMaxPath(root->left, maxPath);         vector<int> rightMaxPath = findMaxPath(root->right, maxPath);         //++tempMaxPath[root->val];                 tempMaxPath[1] = leftMaxPath[1] > rightMaxPath[1] ? leftMaxPath[1]  : rightMaxPath[1] ;         if(root->val == 0)             {             int temp = 0; // tempMaxPath[0] = leftMaxPath[0] + rightMaxPath[0]; temp = leftMaxPath[0] + rightMaxPath[0] + 1; tempMaxPath[0] =  leftMaxPath[0] > rightMaxPath[0] ? leftMaxPath[0] + 1 : rightMaxPath[0] + 1 ; // ++tempMaxPath[0];  if (maxPath < temp)  {  maxPath = temp;  }             tempMaxPath[1] = 0;         }         else             {              int temp = 0; // tempMaxPath[0] = leftMaxPath[0] + rightMaxPath[0]; temp = leftMaxPath[1] + rightMaxPath[1] + 1; tempMaxPath[1] =  leftMaxPath[1] > rightMaxPath[1] ? leftMaxPath[1] + 1 : rightMaxPath[1] + 1 ; // ++tempMaxPath[0]; if (maxPath < temp) { maxPath = temp; } tempMaxPath[0] = 0;         }         return tempMaxPath;     }     int findPath(TreeNode* root) {         // write code here         if(root == NULL)             return 0; int maxPath = 0;          findMaxPath(root, maxPath); return maxPath; //         if(root->val == 0) //             return tempMaxPath[0]; //         else //             return tempMaxPath[1];              } }; 不想说了,自己在电脑上多测了几个样例,想提交第三题,结果说已经结束了,什么鬼!
点赞 评论

相关推荐

Gaynes:查看图片
点赞 评论 收藏
分享
06-10 21:15
门头沟学院 Java
宁阿:好多这种没🧠的公司,他们估计都不知道毕业的人不能给安排实习岗
实习吐槽大会
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务