寻找最长的路径,那么会在左边或者右边或者是从左到跟然后再到右方的路径的。

/**
* 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 res=max(longestUnivaluePath(root->left), longestUnivaluePath(root->right));
return max(res, helper(root->left, root->val)+helper(root->right, root->val));
}
int helper(TreeNode* root, int parent){
if(!root || root->val!=parent) return ;
return +max(helper(root->left, parent), helper(root->right, parent));
}
};

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

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

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

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

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

  4. 【Leetcode_easy】687. Longest Univalue Path

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

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

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

  7. 687. Longest Univalue Path

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

  8. leetcode@ [329] Longest Increasing Path in a Matrix (DFS + 记忆化搜索)

    https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, find the ...

  9. LeetCode #329. Longest Increasing Path in a Matrix

    题目 Given an integer matrix, find the length of the longest increasing path. From each cell, you can ...

随机推荐

  1. Android ORC文字识别之识别身份证号等(附源码)

    项目地址https://github.com/979451341/OrcTest 我们说说实现这个项目已实现的功能,能够截图手机界面的某一块,将这个某一块图片的Bitmap传给tess-two的代码来 ...

  2. java-面向对象(公元2017-6-28)

    1.面向对象 何为面向对象:编写程序的时候会提取相似的 特征,把这些相似的特征组织起来 类:相似的特征组织起来的类型.            泛指.可理解为模板 对象:属于类中的具体事物       ...

  3. JQuery 方法合集(懒人备记)

    原创文章,转载请私信.谢谢~ PS:请将jquery的引用文件放在head的标签内 语法:$(selector).action() $(document).ready(function(){ // 开 ...

  4. 笨办法40模块, 类和对象class

    class a: def change(self, sign, call): self.first = sign self.second = call first = 'first' second = ...

  5. SSM中的Mybatis的操作

    一:整合日志含有log4j和logback logback是log4j的升级版他性能提升较大,有些甚至达到10倍以上,占的内存更小,slf4j能很好的整合它,还有很多数不胜数的优势 1.下载jar包, ...

  6. 学习poisson.c

    static char help[] = "A structured-grid Poisson problem with DMDA+KSP.\n\n"; #include < ...

  7. R语言的精度和时间效率比较(简单版)

    R语言的最大数值 在R语言里面,所能计算的最大数值可以用下面的方法获得: ###R可计算最大数值 .Machine 在编程的时候注意不要超过这个数值.当然,普通情况下也不可能超过的. R语言的最大精度 ...

  8. numpy数据集练习

    #1. 安装scipy,numpy,sklearn包 import numpy as np #2. 从sklearn包自带的数据集中读出鸢尾花数据集data from sklearn.datasets ...

  9. ZABBIX安装过程中relocation error报错解决办法

    错误提示: /usr/sbin/zabbix_server: relocation error: /usr/sbin/zabbix_server: symbol mysql_next_result, ...

  10. 俄罗斯方块win c 图形线程

    环境准备:visual stdio 2015,easyx, //