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 ...
随机推荐
- 利用pom配置实现静态文件拷贝
java项目有时候需要将一些静态文件拷贝到生成的test-class文件夹或者其他地方,虽然手动拷贝可以做到,但是很麻烦.今天主要讲解如何利用pom.xml进行动态的拷贝. 具体的配置信息如下,在de ...
- 如何不使用loop循环创建连续的数组
ES5 Array.apply(null,{length:100}) Object.keys(Array.apply(null,{length:100})); ES6 [...Array(100)]
- 构建web应用之——文件上传
我们通过使用multipart请求数据接收和处理二进制信息(如文件).DispatcherServlet并没有实现任何解析multipart请求数据的功能,它将该任务委托给了Spring中的multi ...
- Mysql基本操作命令【转载】
原文链接:http://www.cnblogs.com/rookie-c/p/6425039.html 创建数据库 CREATE DATABASE name; 显示所有数据库 SHOW DATABAS ...
- Android中软键盘展示、EditText焦点获取及windowSoftInputMode属性探究
2017-08-14 21:44:23 有很多中情况,分别展示. 1.Activity不做任何设置,布局使用LinearLayout 会自动滚动EditText之上的所有View,代码: <?x ...
- Python语言:Day9练习题及其答案
1.使用while循环输出1,2,3,4,5,6, 8,9,10 #!/usr/bin/python3 n = 1 while n <= 10: if n != 7: print(n) else ...
- Android : 高通平台Camera调试之SetpropKey/camxoverridesettings.txt
高通相关网址:Createpoint: https://createpoint.qti.qualcomm.com(可下载文档,Release Note等)Chipcode: https://chipc ...
- Java String 常量池理解
String:字符串常量池 作为最基础的引用数据类型,Java 设计者为 String 提供了字符串常量池以提高其性能,那么字符串常量池的具体原理是什么,我们带着以下三个问题,去理解字符串常量池: 字 ...
- 关于HTMl CSS
HTML 结构 CSS 表现 JS 行为 首先说一个SEO,搜索引擎优化 标准文档流:(1)前提:在没有css的干预下 (2)块级元素:独占一行,可定义宽和 ...
- 小程序 map组件问题 cover-view问题
使用小程序的组件map时 在开发者工具上一切顺利 但是在真机预览时 发现地图的层级是最高的 任何标签都覆盖不了它 调整z-index值并没有什么效果 原因是 微信小程序的map.video.canva ...