/**
* 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. 最长公共子序列lcs 51nod1006

    推荐参考博客:动态规划基础篇之最长公共子序列问题 - CSDN博客  https://blog.csdn.net/lz161530245/article/details/76943991 个人觉得上面 ...

  2. 81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  3. 【校招面试 之 网络】第2题 TCP的可靠传输、流量控制、滑动窗口

    1.可靠传输 (1)三次握手 TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接: (1)第一次握手:建立连接时,客户端A发送SYN包(SYN=j)到服务器B,并进入SYN_S ...

  4. swift - label 的font 设置 文字字体和大小

    设置字体和颜色 lab.textColor = UIColor.init(hexColor: "795928") lab.font = UIFont.systemFont(ofSi ...

  5. zookeeper 单机版配置

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

  6. css外层透明,内层不透明

    外层div透明使用:background: rgba(0, 0, 0, 0.5); 内层div透明使用:opacity:1; 这么样就可以了.不要外层使用opacity,会影响内层也透明的.主要是rg ...

  7. node.js下载安装

    1.下载node.js在node中文网站,官方网站下载太慢 2.接着让我们点击下载链接,页面上呈现出你所需要下载的安装包,我们这里选择windows x64的安装包进行下载 3.安装node.js,一 ...

  8. VS2010,VS2012,VS2013中,无法嵌入互操作类型“……”,请改用适用的接口的解决方法

    在VS2013开发环境下,C#引用COM组件遇到的两个问题: 一开始是COM组件没有注册导致代码引用报错,那么我就使用管理员的命令行方式下,入册COM组件: C:\WINDOWS\system32&g ...

  9. mysql 添加外键

    create table class( cid tinyint unsigned primary key auto_increment, caption varchar(15) not null)en ...

  10. BZOJ2730或洛谷3225 [HNOI2012]矿场搭建

    BZOJ原题链接 洛谷原题链接 显然在一个点双连通分量里,无论是哪一个挖煤点倒塌,其余挖煤点就可以互相到达,而对于一个点双连通分量来说,与外界的联系全看割点,所以我们先用\(tarjan\)求出点双连 ...