Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.

Note: The length of path between two nodes is represented by the number of edges between them.

Example 1:

Input:

              5
/ \
4 5
/ \ \
1 1 5

Output:

2

Example 2:

Input:

              1
/ \
4 5
/ \ \
4 4 5

Output:

2

Note: The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.

Runtime: 72 ms, faster than 28.47% of C++ online submissions for Longest Univalue Path.

对于这种不经过root的求和题往往都需要一个临时变量,然后考虑一下根节点和子节点的关系,用一个引用得到最优解。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int longestUnivaluePath(TreeNode* root) {
int ret = , tmpret = ;
helper(root,tmpret, ret);
return ret == ? : ret - ;
}
int helper(TreeNode* root, int& tmpret, int& ret){
if(!root) return ;
int rl = helper(root->left, tmpret, ret);
int rr = helper(root->right, tmpret, ret);
if(!root->left && !root->right) {
tmpret = ;
//ret = 1;
return ;
} else if(!root->left && root->right){
if(root->val == root->right->val){
tmpret = max(tmpret, rr + );
ret = max(ret, tmpret);
return +rr;
} else return ;
} else if(root->left && !root->right){
if(root->val == root->left->val){
tmpret = max(tmpret, +rl);
ret = max(ret, tmpret);
return +rl;
} else return ;
} else {
if(root->val == root->left->val && root->val == root->right->val){
tmpret = max(tmpret, +rr + rl);
ret = max(ret, tmpret);
return +max(rr,rl);
} else if (root->val == root->left->val){
tmpret = max(tmpret, +rl);
ret = max(ret, tmpret);
return +rl;
} else if (root->val == root->right->val){
tmpret = max(tmpret, +rr);
ret = max(ret, tmpret);
return +rr;
} else return ;
}
}
};

LC 687. Longest Univalue Path的更多相关文章

  1. 【Leetcode_easy】687. Longest Univalue Path

    problem 687. Longest Univalue Path 参考 1. Leetcode_easy_687. Longest Univalue Path; 2. Grandyang; 完

  2. 【LeetCode】687. Longest Univalue Path 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  3. [LeetCode] 687. Longest Univalue Path 最长唯一值路径

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  4. LeetCode 687. Longest Univalue Path 最长同值路径 (C++/Java)

    题目: Given a binary tree, find the length of the longest path where each node in the path has the sam ...

  5. leetcode 687.Longest Univalue Path

    寻找最长的路径,那么会在左边或者右边或者是从左到跟然后再到右方的路径的. /** * Definition for a binary tree node. * struct TreeNode { * ...

  6. 687. Longest Univalue Path

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  7. [LeetCode] 687. Longest Univalue Path_Easy tag: DFS recursive

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  8. [LeetCode] Longest Univalue Path 最长相同值路径

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  9. 最长的相同节点值路径 · Longest Univalue Path

    [抄题]: Given a binary tree, find the length of the longest path where each node in the path has the s ...

随机推荐

  1. JavaScript事件的基本学习

  2. 天线basic

    1.实际应用时,按内置天线还是外置天线考虑. 内置时,净空区在 PCB 上所有层(all layer) 不能放置元件,走线和铺 GND  天线远离金属,至少要距离周围有较高的元器件 10 毫米以上: ...

  3. PAT Basic 1030 完美数列 (25 分)

    给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列. 输入格 ...

  4. 异步消息处理机制相关面试问题-intentservice面试问题详解

    IntentService是什么? IntentService是继承并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService方法和启动传统的S ...

  5. std::map 的swap错用

    map<int, shared_ptr<int>>map_test; shared_ptr<); map_test[] = tmp_1; shared_ptr<); ...

  6. 使用google身份验证器实现动态口令验证

    最近有用户反应我们现有的短信+邮件验证,不安全及短信条数限制和邮件收验证码比较慢的问题,希望我们 也能做一个类似银行动态口令的验证方式.经过对可行性的分析及慎重考虑,可以实现一个这样的功能. 怎么实现 ...

  7. wcPro--WordCount扩展

    Github:https://github.com/whoNamedCody/wcPro PSP表格 PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning 计划     ...

  8. 什么是JWT?Token与Session的区别?

    什么是JWT Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点 ...

  9. ubuntu 添加字体

    1. 下载自己需要安装的字体文件 eg: yaheiconsolashybrid.ttf 2. 将字体文件放在目录/home下 3. 到目录/usr/share/fonts/truetype/下建立目 ...

  10. MessagePack Java Jackson Dataformat 不使用 str8 数据类型的序列化

    老的 msgpack-java(例如 0.6.7)并不支持 MessagePack str8 数据类型. 当你的希望的你的应用程序需要支持老的版本的话,你需要禁用这个数据类型,例如使用下面的语句: M ...