LeetCode-Count Univalue Subtrees
Given a binary tree, count the number of uni-value subtrees.
A Uni-value subtree means all nodes of the subtree have the same value.
For example:
Given binary tree,
5
/ \
1 5
/ \ \
5 5 5
return 4.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public class UniValueCount {
int value;
boolean isUniValue;
int count;
public UniValueCount(int v, boolean is, int num){
value = v;
isUniValue = is;
count = num;
}
}
public int countUnivalSubtrees(TreeNode root) {
UniValueCount res = countUnivalRecur(root);
return res.count;
} public UniValueCount countUnivalRecur(TreeNode cur){
if (cur==null){
return new UniValueCount(0,false,0);
}
if (cur.left==null && cur.right==null){
return new UniValueCount(cur.val,true,1);
} UniValueCount res = new UniValueCount(0,true,0);
if (cur.left!=null){
UniValueCount leftRes = countUnivalRecur(cur.left);
res.isUniValue = res.isUniValue && (leftRes.isUniValue && cur.val==leftRes.value);
res.count += leftRes.count;
}
if (cur.right!=null){
UniValueCount rightRes = countUnivalRecur(cur.right);
res.isUniValue = res.isUniValue && (rightRes.isUniValue && cur.val==rightRes.value);
res.count += rightRes.count;
}
if (res.isUniValue){
res.count++;
res.value = cur.val;
}
return res;
}
}
LeetCode-Count Univalue Subtrees的更多相关文章
- [LeetCode] Count Univalue Subtrees 计数相同值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [Locked] Count Univalue Subtrees
Count Univalue Subtrees Given a binary tree, count the number of uni-value subtrees. A Uni-value sub ...
- [LeetCode] 250. Count Univalue Subtrees 计算唯一值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [LeetCode#250] Count Univalue Subtrees
Problem: Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all ...
- [leetcode]250. Count Univalue Subtrees统计节点值相同的子树
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- 250. Count Univalue Subtrees
题目: Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes ...
- [Swift]LeetCode250.计数相同值子树的个数 $ Count Univalue Subtrees
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [LC] 250. Count Univalue Subtrees
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [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 ...
随机推荐
- Effective Java 06 Eliminate obsolete object references
NOTE Nulling out object references should be the exception rather than the norm. Another common sour ...
- Effective Java 24 Eliminate unchecked warnings
Note Eliminate every unchecked warning that you can. Set<Lark> exaltation = new HashSet(); The ...
- JS 日期对象在浏览器间的若干差异
JS中 ,通过 new Date() 可以获取当前时间 也可以通过 new Date("2013/12/12 8:00:00")的方式,创建某个指定时间对象 在Chrome和FF下 ...
- MySQL 存储过程实例 与 ibatis/mybatis/hibernate/jdbc 如何调用存储过程
虽然MySQL的存储过程,一般情况下,是不会使用到的,但是在一些特殊场景中,还是有需求的.最近遇到一个sql server向mysql迁移的项目,有一些sql server的存储过程需要向mysql迁 ...
- php多线程pthreads的安装与使用
安装Pthreads 基本上需要重新编译PHP,加上 --enable-maintainer-zts 参数,但是用这个文档很少:bug会很多很有很多意想不到的问题,生成环境上只能呵呵了,所以这个东西玩 ...
- 烂泥:LVM学习之逻辑卷及卷组缩小空间
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 上一篇文章,我们学习了如何给LVM的逻辑卷及卷组扩容.这篇文章我们来学习,如何给LVM的逻辑卷及卷组缩小空间. 注意逻辑卷的缩小一定要离线操作,不能是在 ...
- Android adb 无线调试
转自:使用WIFI连接android进行调试和adb操作 1. 手机端开启adb tcp连接端口,下载android终端app(终端模拟器) :/$su:/$setprop service.adb.t ...
- 关于hadoop 配置文件的一些实验
机器配置如下,两台机器,nn2,nn2,搭建基于QJM的高可用集群,zk集群. 如果我在yarn-site.xml中配置的nodemanager的可用资源过少,其他配置如果不一致,那么就会造成提交的j ...
- 详解Webwork中Action 调用的方法
详解Webwork中Action 调用的方法 从三方面介绍webwork action调用相关知识: 1.Webwork 获取和包装 web 参数 2.这部分框架类关系 3.DefaultAction ...
- hdu-5904 LCIS(水题)
题目链接: LCIS Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...