题目:

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.

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.

分析:

给定一颗二叉树,求最长的路径,其中路径是相同节点间的边数。

递归求解此问题,对于每一个结点,我们要求此时最大的路径数,而最大路径数是由其左右两个孩子结点决定的,那么递归的终止条件就是当为空结点时,路径数为0,当我们知道左右孩子的最大路径数时,需要判断当前和左右孩子结点是否相同,如果相同则当前的结点的最大路径数应该为左孩子路径数+1/右孩子路径数+1取最大值,同时更新结果为当前结果和以当前结点根结点时的左右孩子路径数的和的最大值。

程序:

C++

/**
* 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 res = ;
if(root == nullptr)
return res;
univaluePath(root, res);
return res;
}
private:
int univaluePath(TreeNode* root, int& res){
if(root == nullptr)
return ;
int l = univaluePath(root->left, res);
int r = univaluePath(root->right, res);
int pl = ;
int pr = ;
if(root->left != nullptr && root->val == root->left->val)
pl = l + ;
if(root->right != nullptr && root->val == root->right->val)
pr = r + ;
res = max(res, pl + pr);
return max(pl, pr);
}
};

Java

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int longestUnivaluePath(TreeNode root) {
if(root == null)
return res;
univaluePath(root);
return res;
}
private int res = 0;
private int univaluePath(TreeNode root){
if(root == null)
return 0;
int l = univaluePath(root.left);
int r = univaluePath(root.right);
int pl = 0;
int pr = 0;
if(root.left != null && root.val == root.left.val)
pl = l + 1;
if(root.right != null && root.val == root.right.val)
pr = r + 1;
res = Math.max(res, pl + pr);
return Math.max(pl, pr);
} }

最新文章

  1. 黑马程序员——【Java高新技术】——类加载器
  2. 威纶触摸屏和三菱PLC3S之间的通信设置
  3. Oracle 11.2.4.0 ACTIVE DATAGUARD 单实例安装(COPY创建备库)
  4. loj 1044(dp+记忆化搜索)
  5. 基于soapUI构建WebService测试框架
  6. 快速了解AngularJs HTTP响应拦截器
  7. mysql创建表与索引
  8. 知识总结: Activity的四种启动模式
  9. keepavlied一些参数
  10. Myeclipse或Eclipse中搭建Easyui环境
  11. 免费搭建wordpress博客有感
  12. Tomcat代码执行漏洞(CVE-2017-12615)的演绎及个人bypass
  13. Dynamics CRM2013 Lookup Filtering using addCustomFilter
  14. js实现点气球小游戏
  15. VMware手动添加centos7硬盘图文操作及分区超详细
  16. Linux中各个文件的作用
  17. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
  18. 获取Tomcat更详细的日志
  19. 【转载】koa相关知识(来自官网)
  20. 【BZOJ4820】[SDOI2017]硬币游戏(高斯消元)

热门文章

  1. 提升Windows系统舒适度软件
  2. 51nod 1559 车和矩形
  3. mysql更新某一列数据
  4. flask部署:Ubuntu下使用nginx+uwsgi+supervisor部署flask应用
  5. eclipse 安装spring tools suite插件
  6. mapper.xml实现oracle的分页语句
  7. python 编写程序输出50以内勾股数,如下图所示,要求每组显示六祖,各组勾股数无重复
  8. 关于C#设计SQllite
  9. Bean XML 配置(1)- 通过XML配置加载Bean
  10. junit基础学习之-简介(1)