/**
* 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) {
if (!root) return ;
int max = ;
dfs(root, max);
return max;
} int dfs(TreeNode* root, int & max) {
int l = , r = ; if (root->left && root->left->val == root->val)
l = + dfs(root->left, max);
else if (root->left)
dfs(root->left, max); if (root->right && root->right->val == root->val)
r = + dfs(root->right, max);
else if (root->right)
dfs(root->right, max); if (l+r > max)
max = l+r; return (l > r) ? l : r;
}
};

补充一个python的实现:

 class Solution:
def __init__(self):
self.path = def lastOrder(self,root):
if root == None:
return
left = self.lastOrder(root.left)
right = self.lastOrder(root.right)
leftpath,rightpath = ,
if root.left != None:
leftpath = left + if root.val == root.left.val else
if root.right != None:
rightpath = right + if root.val == root.right.val else
self.path = max(self.path,leftpath+rightpath)
return max(leftpath,rightpath) def longestUnivaluePath(self, root: TreeNode) -> int:
self.lastOrder(root)
return self.path

leetcode687的更多相关文章

  1. [Swift]LeetCode687. 最长同值路径 | Longest Univalue Path

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

  2. LeetCode--687. 最长同值路径

    题目描述:给定一个二叉树,找到最长的路径,这个路径中的每个节点具有相同值.这条路径可以经过也可以不经过根节点.注意:两个节点之间的路径长度由它们之间的边数表示. 示例1:输入: 5 / \ 4 5 / ...

  3. Leetcode687.Longest Univalue Path最长同值路径

    给定一个二叉树,找到最长的路径,这个路径中的每个节点具有相同值. 这条路径可以经过也可以不经过根节点. 注意:两个节点之间的路径长度由它们之间的边数表示. 示例 1: 输入: 5 / \ 4 5 / ...

随机推荐

  1. mysql时间与字符串的互转

    将时间转换为字符串select date_format(now(), '%Y%m%d%H%i%s'), now(); 将字符串转换为时间select str_to_date('201901131111 ...

  2. BZOJ5280: [Usaco2018 Open]Milking Order(二分+拓扑)

    5280: [Usaco2018 Open]Milking Order Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 123  Solved: 62[ ...

  3. onerror="javascript:this.src='images/defaultUpload.png';"引发的死循环错误

    18:10:47.441 WARN o.s.web.servlet.PageNotFound:1101 - No mapping found for HTTP request with URI [/c ...

  4. Filter学习(二)Filter使用Decorator设计模式

    在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目 ...

  5. ASP.NET Core 中的SEO优化(1):中间件实现服务端静态化缓存

    分享 最近在公司成功落地了一个用ASP.NET Core 开发前台的CMS项目,虽然对于表层的开发是兼容MVC5的,但是作为爱好者当然要用尽量多的ASP.NET Core新功能了. 背景 在项目开发的 ...

  6. pat 乙级 1093 字符串A+B (20 分)

    给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集.要求先输出 A,再输出 B,但重复的字符必须被剔除. 输入格式: 输入在两行中分别给出 A 和 B,均为长度不超过 1的.由可见 ...

  7. Python基础之路

    一.Python基础之简介 二.Python基础之数据类型 三.Python之运算符 三.Python变量 四.Python之流程控制 三.Python基础之函数 四.Python基础之面向对象

  8. 解决js代码中加入alert()就成功执行,不加就不对的问题!

    问题: jquery中的$(document).ready(function(){})中调用两个方法(1)利用ajax请求去后台查图书类别的方法(2)当页面上利用图书类别去查询图书返回页面,让图书类别 ...

  9. Gridview实现突出显示某一单元格的方法

    GridView突出显示某一单元格(例如金额低于多少,分数不及格等) 效果图: 解决方案:主要是绑定后过滤 GridView1.DataBind();        for (int i = 0; i ...

  10. matplotlib ----- 多子图, subplots

    这一篇讲的比较详细. http://matplotlib.org/examples/pylab_examples/subplots_demo.html 官方文档给出的subplots用法, http: ...