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的更多相关文章

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

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

  5. leetcode 687.Longest Univalue Path

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

  6. 687. Longest Univalue Path

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * 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. 最长的相同节点值路径 · Longest Univalue Path

    [抄题]: Given a binary tree, find the length of the longest path where each node in the path has the s ...

随机推荐

  1. linux 网卡相关命令

    1. ifconfig //查看网络相关信息 2. ifconfig eth0 192.168.1.103 netmask 255.255.255.0 //配置eth0的IP地址 3. route - ...

  2. deep_learning_cross_entropy

    交叉熵损失函数 交叉熵代价函数(Cross-entropy cost function)是用来衡量人工神经网络(ANN)的预测值与实际值的一种方式.与二次代价函数相比,它能更有效地促进ANN的训练.在 ...

  3. string类的总结

    一.string类头文件:#include <string>;using namespace std; 二.string类方法: 1.获取string的字符串长度:size(),size返 ...

  4. 三、Nginx支持php

    一.php fastcgi配置(分析宝塔的) 因为只是配置PHP,所以只需要改动server部分 关于phpmyadmin这个站点的信息: 查找该文件:

  5. umount 问题

    使用 umount -f PATH 卸载 ➜ ~ cd ➜ ~ umount -f /webserver/page 先 cd 到家目录, 再强制卸载, 如果执行此命令后, 还是提示磁盘"bu ...

  6. nginx基本概述

    上级回顾: 1.NFS 2.Sersync 3.SSH 1.ssh使用两种登录服务器的方式,哪两种? 密码 用户名 + 密码 秘钥 用户名 + 秘钥(私钥) 公钥加密 私钥解密 2.大家常说的 塞ke ...

  7. .NET Core 3时代如何转换.NET 4项目,DevExpress Winforms帮你忙!

    DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅.美观且易于使用的应用程序.无论是Office风格的界面,还是分析处理大批量的业务数据,DevExpr ...

  8. 小程序是单页面应用,有一个页面执行wx.showLoading(),其他页面也会显示

    my.js onLoad: function (options) { setTimeout(res=>{ wx.showLoading({ title: '10s后出现', }) }, ) }, ...

  9. BZOJ 1420: Discrete Root (原根+BSGS)

    题意 已知kkk, aaa, ppp. 求 xk≡a (mod p)x^k\equiv a\ (mod\ p)xk≡a (mod p) 的所有根. 根的范围[0,p−1][0,p-1][0,p−1]. ...

  10. 【GDKOI2018】总结

    前言 车祸现场... day1 T1:其实就是对于每个点的有用的时间点建一个点,然后连边,对于询问(x,y),从点(y,inf),往回走,能走到的最早的x的时间点就是答案. 比赛上用最后的一个多小时来 ...