关注
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];
}
};
不想说了,自己在电脑上多测了几个样例,想提交第三题,结果说已经结束了,什么鬼!
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 26届的你,投了哪些公司? #
13572次浏览 164人参与
# 我对___祛魅了 #
23980次浏览 247人参与
# 中兴秋招 #
191738次浏览 2148人参与
# 你最讨厌面试问你什么? #
9549次浏览 152人参与
# 你跟室友的关系怎么样? #
2546次浏览 57人参与
# 工作中哪个瞬间让你想离职 #
43100次浏览 371人参与
# 简历上的经历如何包装 #
9361次浏览 281人参与
# 通信/硬件求职避坑tips #
85888次浏览 868人参与
# 如何快速融入团队? #
8754次浏览 104人参与
# 和同事相处最忌讳的是__ #
11805次浏览 124人参与
# 你遇到最难的面试题目是_ #
3313次浏览 69人参与
# 什么样的背景能拿SSP? #
13763次浏览 116人参与
# 应届生应该先就业还是先择业 #
124571次浏览 695人参与
# 字节跳动工作体验 #
457998次浏览 4623人参与
# 我和mentor的爱恨情仇 #
61606次浏览 377人参与
# 如何排解工作中的焦虑 #
190258次浏览 1957人参与
# 元戎启行求职进展汇总 #
35818次浏览 276人参与
# 总结:哪家公司面试体验感最差 #
63029次浏览 286人参与
# 应届生进小公司有什么影响吗 #
85368次浏览 1053人参与
# 打工人的精神状态 #
66774次浏览 1099人参与
# 实习生活中那些难忘的瞬间 #
162718次浏览 2425人参与