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.

Solution:
/**
* 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的更多相关文章

  1. [LeetCode] Count Univalue Subtrees 计数相同值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  2. [Locked] Count Univalue Subtrees

    Count Univalue Subtrees Given a binary tree, count the number of uni-value subtrees. A Uni-value sub ...

  3. [LeetCode] 250. Count Univalue Subtrees 计算唯一值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  4. [LeetCode#250] Count Univalue Subtrees

    Problem: Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all ...

  5. [leetcode]250. Count Univalue Subtrees统计节点值相同的子树

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  6. 250. Count Univalue Subtrees

    题目: Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes ...

  7. [Swift]LeetCode250.计数相同值子树的个数 $ Count Univalue Subtrees

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  8. [LC] 250. Count Univalue Subtrees

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

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

随机推荐

  1. 小波说雨燕 第三季 构建 swift UI 之 UI组件集-视图集(一)视图共性 学习笔记

    如果想进行自定义的配置,可以继承基类UIView. 地图app中需要多点触动Multiple Touch, opaque不透明的 hidden隐藏的 比如下载的进度条,如果下载完毕,可以通过设置这个属 ...

  2. 解决Junit单元测试 找不到类 ----指定Java Build Path

    做junit 单元测试时,发现怎么执行都是以前编译过得代码. 最后找到原因了, src/test/java 编译完的.class路径是 Default output folder Default ou ...

  3. ASP出500错误怎么办(理论上并不止500错误,其他错误可以同样可以获得更多信息以帮助解决问题)

    造成500错误常见原因有:ASP语法出错.ACCESS数据库连接语句出错.文件引用与包含路径出错.使用了服务器不支持的组件如FSO等. 为了定位500错误的具体原因,可以这样做: 让IE显示详细的出错 ...

  4. olacle数据库员工表的创建,及插入数据,添加约束,删除列,查询数据的sql语句

    ---删除原有的员工表drop TABLE employee;---创建员工表CREATE TABLE employee       (       empno NUMBER(4) NOT NULL, ...

  5. java web中日期Date类型在页面中格式化显示的三种方式

    一般我们经常需要在将服务器端的Date类型,传到页面进行显示,这就涉及到一个如何格式化显示Date类型的问题,一般我们有三种方式进行: 1)在服务端使用SimpleDateFormat等类格式化成字符 ...

  6. 【mysql】索引的优化

    写在前面的话 查询容易,优化不易,且写且珍惜 mysql结构 从MySQL逻辑架构来看,MySQL有三层架构,第一层连接,第二层查询解析.分析.优化.视图.缓存,第三层,存储引擎 MySQL有哪些索引 ...

  7. 不用synchronized块的话如何实现一个原子的i++?

    上周被问到这个问题,没想出来,后来提示说concurrent包里的原子类.回来学习一下. 一.何谓Atomic? Atomic一词跟原子有点关系,后者曾被人认为是最小物质的单位.计算机中的Atomic ...

  8. jmeter接口测试教程

    一.Jmeter接口测试步骤: 1.添加线程组

  9. redis的info

    redis的info     http://redis.readthedocs.org/en/latest/server/info.html   INFO   INFO [section]   以一种 ...

  10. sql 查询基本语法

    1.计算列        select * from emp           --*表示所有的           --from emp 表示从emp表查询   select empno,enam ...