/**
* 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. Tensorflow函数——tf.variable_scope()

    Tensorflow函数——tf.variable_scope()详解 https://blog.csdn.net/yuan0061/article/details/80576703 2018年06月 ...

  2. git 基本操作命令

    1. git status 查看git 状态 2.git init 3.git push -u origin master 提交 4.git remote set "邮箱地址i" ...

  3. PTA 7-7 六度空间(广搜)

    “六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论.这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够 ...

  4. f5电源模块损坏

    现象: ssh登录f5后有日志显示 现场确认f5 2槽电源指示灯不亮,且电源线正常.重新开关电源模块,拔插电源线后仍不亮.故确认是电源模块损坏. 处理:直接采购新的电源模块更换即可.f5电源支持热插拔 ...

  5. zookeeper 单机版配置

    zookeeper :中间件,为分布式系统进行协调服务 作用于分布式系统,可以为大数据服务 支持java 和 C 客户端的api zookeeper 特性:一致性,数据会按照顺序分批入库: 原子性:数 ...

  6. android的Afinal框架下的数据库更新

    项目需要,版本升级时给表添加了一些新的字段,发现出现异常. 解决方法:监听数据库的版本号,更新数据库. 创建FinalDb对象时使用如下的构造函数,监听版本号的变化: db = FinalDb.cre ...

  7. Bootstrap(3) 表格与按钮

    1.表格 基本格式,实现基本的表格样式 <table class="table"> <thead> <tr> <th>编号</ ...

  8. (五)ROS节点

    一. 理解ROS 节点: ROS的节点: 可以说是一个可运行的程序.当然这个程序可不简单.因为它可以接受来自ROS网络上其他可运行程序的输出信息,也可以发送信息给ROS网络,被其他 ROS 可运行程序 ...

  9. Web API中常用Filter的执行顺序举例讲解

    在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行过程拦截处理.引入了这一机制可以更好地践行DRY(Don’t Repeat Yourself)思想 ...

  10. sqlserver中为节约存储空间的收缩数据库机制

    1.收缩数据库: 删除数据库的每个文件中已经分配单还没有使用的页,首座后数据库空间自动减少 2.收缩方式: (1)自动收缩数据库 选中数据库--->右击--->属性 在常规这里我们可以看到 ...