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 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.
Runtime: 72 ms, faster than 28.47% of C++ online submissions for Longest Univalue Path.
对于这种不经过root的求和题往往都需要一个临时变量,然后考虑一下根节点和子节点的关系,用一个引用得到最优解。
/**
* 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) {
int ret = , tmpret = ;
helper(root,tmpret, ret);
return ret == ? : ret - ;
}
int helper(TreeNode* root, int& tmpret, int& ret){
if(!root) return ;
int rl = helper(root->left, tmpret, ret);
int rr = helper(root->right, tmpret, ret);
if(!root->left && !root->right) {
tmpret = ;
//ret = 1;
return ;
} else if(!root->left && root->right){
if(root->val == root->right->val){
tmpret = max(tmpret, rr + );
ret = max(ret, tmpret);
return +rr;
} else return ;
} else if(root->left && !root->right){
if(root->val == root->left->val){
tmpret = max(tmpret, +rl);
ret = max(ret, tmpret);
return +rl;
} else return ;
} else {
if(root->val == root->left->val && root->val == root->right->val){
tmpret = max(tmpret, +rr + rl);
ret = max(ret, tmpret);
return +max(rr,rl);
} else if (root->val == root->left->val){
tmpret = max(tmpret, +rl);
ret = max(ret, tmpret);
return +rl;
} else if (root->val == root->right->val){
tmpret = max(tmpret, +rr);
ret = max(ret, tmpret);
return +rr;
} else return ;
}
}
};
LC 687. Longest Univalue Path的更多相关文章
- 【Leetcode_easy】687. Longest Univalue Path
problem 687. Longest Univalue Path 参考 1. Leetcode_easy_687. Longest Univalue Path; 2. Grandyang; 完
- 【LeetCode】687. Longest Univalue Path 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- [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
寻找最长的路径,那么会在左边或者右边或者是从左到跟然后再到右方的路径的. /** * Definition for a binary tree node. * struct TreeNode { * ...
- 687. Longest Univalue Path
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- [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 ...
- [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 ...
- 最长的相同节点值路径 · Longest Univalue Path
[抄题]: Given a binary tree, find the length of the longest path where each node in the path has the s ...
随机推荐
- FileStrem大文件分割复制
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- jQuery获取表单全部数据
iQuery如何获取表单的全部数据,用于ajax提交 var formData = {}; var t = $('#Form').serializeArray(); $.each(t, functio ...
- PLS做soft particle
这个pixel local storage frame fetch 可以一个pass做出soft particle/deferred lighting/soft edge water programb ...
- kylin实战(一)
kylin适用场景 OLAP 它适合数据量大,查询维度多,但是业务改动不频繁的场景.因为业务多,则kylin的cube很多.每次业务变更,kylin修改的工作量大,且每次全量跑数据耗费时间比较长. 它 ...
- Windows netsh命令的使用
Windows netsh命令 netsh(也被称为网络壳层),是一个存在于自微软 Windows 20000开始的所有Windows NT系列中的命令行工具. netsh允许本地或远程配置网络设备. ...
- Fetch(原生)的简单使用
前言: Fetch 提供了对 Request 和 Response 等对象通用的定义. 发送请求或者获取资源,需要使用 fetch() 方法. 具体使用代码: <script> fetch ...
- react-缓存
目录结构: 用到缓存的地方: 缓存的方法 清楚缓存
- sqoop参数详解
从RDBMS到HIVE: sqoop import --connect jdbc:oracle:thin:@//192.168.156.111/test--username test --passwo ...
- yii 上传视频(ajax)
实现一个功能:提交表单的时候,需要上传视频,把视频上传到oss上,然后把url作为表单值传到后端保存到数据库.需要ajax异步实现. 遇到了一个这样报错:Bad Request: 您提交的数据无法被验 ...
- 牛客训练41D最小相似度bfs
最小相似度 题目大意:对于位数相同的两个二进制串,SIM(A,B)为它们的相似度,也就是A^B中0的个数.现在给定一堆串,找出一个T使得max{SIM(S1,T),SIM(S2,T),......,S ...