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.

class Solution {
public:
int longestUnivaluePath(TreeNode* root) {
int lup = ;
if (root) dfs(root, lup);
return lup;
} private:
int dfs(TreeNode* node, int& lup) {
int l = node->left ? dfs(node->left, lup) : ;
int r = node->right ? dfs(node->right, lup) : ;
int resl = node->left && node->left->val == node->val ? l + : ;
int resr = node->right && node->right->val == node->val ? r + : ;
lup = max(lup, resl + resr);
return max(resl, resr);
}
};

Recursion-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. LC 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 最长唯一值路径

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

  5. 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 ...

  6. leetcode 687.Longest Univalue Path

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

  7. 687. Longest Univalue Path

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

  8. [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 ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. 调用数据库--stone

    from Mysql_operate_class import mysql def saveMysqlData(sql, dbname="algorithm"): pym = my ...

  2. javac 编译java文件提示: 程序包com.mysql.jdbc不存在

    需要将引用的包放到:/usr/java/jdk1.7.0_75/jre/lib/ext 也就是jdk安装目录/jre/lib/ext   目录下面

  3. Metro Revealed: Building Windows 8 apps with XAML and C# 阅读笔记

    第一章1.1.3中提到 Jesse Liberty 的<Pro Windows 8 Development with XAML and C#>,这是一本关于win8更全面的书,以后看.

  4. using directive 使用指令,与using declaration使用声明。

    使用指令是把名字空间中的所有名字引入到当前作用域,而使用声明是把名字空间的某个名字引入到当前作用域中 语法如下 //test.cpp #include<iostream> //using ...

  5. 2018.10.20 loj#2593. 「NOIP2010」乌龟棋(多维dp)

    传送门 f[i][j][k][l]f[i][j][k][l]f[i][j][k][l]表示用iii张111,jjj张222,kkk张333,lll张444能凑出的最大贡献. 然后从f[i−1][j][ ...

  6. hibernate createQuery和createSQLQuery 查询结果count计算

    createQuery 针对hql语句查询 Query query=getSession().createQuery(hql);int result =((Number) query.iterate( ...

  7. 用node.js写个在Bash上对字符串进行Base64或URL的encode和decode脚本

    一:自己这段时间经常要用到Base64编码和URL编码,写个编译型语言有点麻烦干脆就用node.js弄了个,弄好后在/etc/profile里加上alias就能完成工具的配置,先上代码: functi ...

  8. Vivado级联Modelsim仿真Re-launch问题

    前两天在群里看到有朋友说Vivado级联Modelsim仿真出现修改设计代码后重新run do文件,波形没有随着代码修改而改变,这个问题博主之前没有注意到,因为把Vivado和Modelsim级联好后 ...

  9. jquery 问题

    detach():这个方法不会把匹配的元素从jQuery对象中删除,因而可以在将来再使用这些匹配的元素.与remove()不同的是,所有绑定的事件.附加的数据等都会保留下来. jquery ajax不 ...

  10. 团队项目第六周——Alpha阶段项目复审(名字很难想队)

    Alpha阶段项目复审 小组 优点 缺点 排名 小谷围驻广东某工业719电竞大队 一个贴近大学生生活的二手交易平台.界面美观功能完善. 部分功能未完善,没有第三方登录 1 中午吃啥队 系统完善,界面简 ...