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 ...
随机推荐
- JavaMaven【二、目录结构&HelloMaven】
目录结构 Demo: 1.创建目录结构 2.编写hello maven以及对应的test,放到对应目录下 package com.shane.maven01.model; public class H ...
- 什么是PAM认证
PAM(Pluggable Authentication Modules )是由 Sun 提出的一种用于实现应用程序的认证机制.其核心是一套共享库,目的是提供一个框架和一套编程接口,将认证工作由程序员 ...
- Oracle笔记(五) 单行函数
虽然各个数据库都是支持SQL语句的,但是每一个数据库也有每一个数据库自己所支持的操作函数,这些就是单行函数,而如果要想进行数据库开发的话,除了要会使用SQL之外 ,就是要多学习函数. 单行函数主要分为 ...
- python函数:装饰器、修正、语法糖、有参装饰器、global与nonlocal
一.装饰器 二.装饰器修正1 三.装饰器修正2 四.装饰器的语法糖 五.有参.无参装饰器 六.global与nonlocal 一.装饰器 ''' 1 什么是装饰器 器=>工具 装饰=>指的 ...
- Hadoop_07_HDFS的Java API 操作
通过Java API来访问HDFS 1.Windows上配置环境变量 解压Hadoop,然后把Hadoop的根目录配置到HADOOP_HOME环境变量里面 然后把HADOOP_HOME/lib和HAD ...
- STM32/EMC/ZILIAO
https://www.st.com/content/ccc/resource/technical/document/application_note/a2/9c/07/d9/2a/b2/47/dc/ ...
- javascript代码实用方法实现
javascript代码实用方法实现 针对现在大家平时开发中,都会写一些重复性的js处理代码,今天总结了几个比较常用的方法实现.获取get请求参数.去字符串空格. 1.获取get请求中的参数 ...
- python 示例代码4
示例:用户输入和格式化输出(用户输入demo1)
- IntelliJ IDEA导包快捷键
IntelliJ IDEA导包快捷键 Alt+Enter
- JS 函数相关的声明调用
// 函数声明方法一 function f (a, b) { return a + b; } // 函数调用 console.log(f(1, 4)); // 函数声明方法二 var num = fu ...