/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int res=;
int longestUnivaluePath(TreeNode* root)
{
if(root==NULL)
return ;
count(root);
return res;
} int count(TreeNode* root)
{
int l=root->left?count(root->left):;
int r=root->right?count(root->right):;
int resl=root->left&&root->left->val==root->val?l+:;
int resr=root->right&&root->right->val==root->val?r+:;
res=max(res,resl+resr);
return max(resl,resr);
}
};

这个题要注意一下,容易错,辅助遍历函数有返回值。

687. Longest Univalue Path的更多相关文章

  1. 【Leetcode_easy】687. Longest Univalue Path

    problem 687. Longest Univalue Path 参考 1. Leetcode_easy_687. Longest Univalue Path; 2. Grandyang; 完

  2. 【LeetCode】687. Longest Univalue Path 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. leetcode 687.Longest Univalue Path

    寻找最长的路径,那么会在左边或者右边或者是从左到跟然后再到右方的路径的. /** * Definition for a binary tree node. * struct TreeNode { * ...

  7. [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 ...

  8. [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 ...

  9. [Swift]LeetCode687. 最长同值路径 | Longest Univalue Path

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

随机推荐

  1. Toolbar 工具栏 菜单 标题栏 Menu

    要使用Toolbar,要先将标题栏(ActionBar)关掉: style.xml中:<style name="MainActivityTheme" parent=" ...

  2. ASP.Net MVC 在ajax接收controller返回值为Json数据

    首先,再次回忆一下ajax的标准用法:(这张图写的比较详细了)(转) 页面部分ajax代码: $.ajax({            url: "/Home/Login?account=&q ...

  3. 安卓操作系统版本(Version)与应用程序编程接口等级(Application Programming Interface Level)对照表

    Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑. 使用Android API,可以在Java环境开发App,编译.打包后可在Android系统 ...

  4. The following packages have unmet dependencies错误

    当出现类似这类错误: The following packages have unmet dependencies: python-dev : Depends: python (= 2.7.5-5ub ...

  5. TZOJ 2546 Electricity(去掉割点后形成的最大连通图数)

    描述 Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The compa ...

  6. HUABASE :基于列存储的关系型数据库系统

    摘要   HUABASE 是基于列存储的关系型数据库系统.列存储技术的特点是数据查询效率高,读磁盘少,存储空间少,是构建数据仓库的理想架构. HUABASE 实现了多种数据压缩机制.查询优化和稀疏索引 ...

  7. git中 vi/vim的命令

    一.vi & vim 有两种工作模式: 1.命令模式:接受.执行 vi操作命令的模式,打开文件后的默认模式: 2.编辑模式:对打开的文件内容进行 增.删.改 操作的模式: 在编辑模式下按下ES ...

  8. Vue vue.extend 和vue.component 两则之间的区别

    Vue.extend 返回的是一个 扩展实例构造器, 也就是一个预设了部分选项的Vue实例构造器 Var myExtend = Vue.extend({ //预设选项 })//返回一个 扩展实例构造器 ...

  9. python 面向对象编程 之 单例模式

    单例模式三种实现方式: 单例模式:单例模式是解决系统资源浪费的一种方案,是指一个类实例化后可以多次使用此对象. 单例模式应用场景:数据库操作.日志.后台打印 # settings.py# Host=' ...

  10. SQL查询有两门以上不及格的学生及查询出全部及格的学生

    1.表结构: /*学生*/ create table student( sno int not null primary key, sname ) ); /*课程*/ create table cen ...