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

/**
* 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. JeasyUI,导出Excel

    这个是客户端表格导出伪Excel文档. 不知道为啥,超过200条,不能导出,显示网络错误 $.extend($.fn.datagrid.methods, { //超过200条,不能导出,显示网络错误? ...

  2. Phoenix安装

    第一步: 安装erlang虚拟机: 第二步: 安装Elixir Add Erlang Solutions repo: wget https://packages.erlang-solutions.co ...

  3. sqlserver常用存储过程基本语法

    一.定义变量--简单赋值 declare @a intset @a=5 print @a --使用select语句赋值 declare @user1 nvarchar(50) select @user ...

  4. 部署你的CRM程序

    教你发布CRM   发布CRM你将使用以下软件 nginx uWSGI CentOS7 CRM项目文件 virtualenv supervisor WSGI.uWSGI python web服务器开发 ...

  5. Python内存释放

    python自己管理内存,实际上,对于占用很大内存的对象,并不会马上释放. 举例,a=range(10000*10000),会发现内存飙升一个多G,del a 或者a=[]都不能将内存降下来.. de ...

  6. Java中的volatile变量有什么作用?

    vlolatile是一个特殊的的修饰符,只能修饰成员变量,在Java并发程序缺少同步类的情况下,多线程对成员变量的操作对其他线程是透明的.volatilel变量可以保证下一个读取操作会在前一个写操作之 ...

  7. 阻塞队列 - java基于链表的简单实现

    1.阻塞队列的原理 阻塞队列与普通队列的区别在于:阻塞队列为空时,从队列中获取元素的操作将会被阻塞,当队列为满时,往队列里添加元素的操作会被阻塞. 试图从空的阻塞队列中获取元素的线程将会被阻塞,直到其 ...

  8. 关于Django的配置

    一. 下载与安装Django             登录Django的官网,https://www.djangoproject.com/download/,在网页的左边有下载压缩包,下载后解压在py ...

  9. Main Steps to Setup an ODI data sync

    0. Get ODI installed 1. Topo physical Architecture/new physical schema 2. New Logical schema 3. New ...

  10. μCOS-Ⅲ——临界段

    临界段代码(critical sections),也叫临界区(critical region),是指那些必须完整连续运行,不可被打断的代码段.μC/OS-Ⅲ系统中存在大量临界段代码.采用两种方式对临界 ...