leetcode 687.Longest Univalue Path
寻找最长的路径,那么会在左边或者右边或者是从左到跟然后再到右方的路径的。
/**
* 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的更多相关文章
- [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 ...
- 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 ...
- 【LeetCode】687. Longest Univalue Path 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- 【Leetcode_easy】687. Longest Univalue Path
problem 687. Longest Univalue Path 参考 1. Leetcode_easy_687. Longest Univalue Path; 2. Grandyang; 完
- 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 ...
- [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 ...
- 687. Longest Univalue Path
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- 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 ...
- 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 ...
随机推荐
- 《程序设计入门——C语言》翁恺老师 第五周编程练习记录
1 素数和(5分) 题目内容: 我们认为2是第一个素数,3是第二个素数,5是第三个素数,依次类推. 现在,给定两个整数n和m,0<n<=m<=200,你的程序要计算第n个素数到第m个 ...
- 关于angular 的路由title设置
在主模块下 constructor( private router: Router, private activatedRoute: ActivatedRoute, ) {} this.router. ...
- Vscode下的Markdown的基本使用
1.Vscode默认支持Markdown语法,只需要安装相应的扩展插件,Markdown Preview enhanced. 2.安装完插件后,在Vscode上新建一个文件,然后将文件的语言模式设置为 ...
- FX-玩列表
list = []while True: meus = ("1.查看","2.添加","3.删除","0.退出") pr ...
- C#保留小数点后几位
String.Format("{0:N1}", a) 保留小数点后一位 String.Format("{0:N2}", a) 保留小数点后两位 String.F ...
- 小飞侠带你精通Python网络编程系列03-Python版本的选择
1. 目前Python有两个主要版本Python2.X和Python3.X 2. Python2.X最后一个版本是2.7,目前(2018年10月21日)Python3.X最新版本为3.7 3. 很不幸 ...
- nginx配置ssl证书
一:加装nginx的ssl模块 1.1:切换到源码包 cd /zz/nginx-1.14.2 1.2:查看已安装模块 /usr/local/nginx/sbin/nginx -V [root@game ...
- C#的ArrayList与JS的push,转字符串逗号分隔
拼接字符串是常用的基本代码,但是还是有很多人习惯用"+"拼接字符串,这样做有以下缺点: 1)为了去掉尾部(或头部)分隔符,写法复杂 2)容易出现BUG报错,比如空值 C#中推荐大家 ...
- Oracle数据库各种名字的区别
数据库名(DB_NAME).数据库实例名(INSTANCE_NAME).操作系统环境变量(ORACLE_SID).数据库服务名(SERVICE_NAME).数据库域名(DB_DOMAIN)以及全局数据 ...
- vue-baidu-map 的简单使用
首先附上vue-baidu-map 文档地址: https://dafrok.github.io/vue-baidu-map/#/zh/index 1.安装,初步使用,文档说的都很明白,就不在过多重复 ...