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. MySQL学习总结(摘抄)

    1.数据库概述 简 而言之,数据库(DataBase)就是一个存储数据的仓库.为了方便数据的存储和管理,将数据按照特定的规律存储在磁盘上.通过数据库管理系统,可以有 效的组织和管理存储在数据库中的数据 ...

  2. [转]Designing a User Interface

    UI design can be divided into three essential elements : functionality, aesthetics, and performance. ...

  3. ORACLE 创建与使用视图

    一.what(什么是视图?) 1.视图是一种数据库对象,是从一个或者多个数据表或视图中导出的虚表,视图所对应的数据并不真正地存储在视图中,而是存储在所引用的数据表中,视图的结构和数据是对数据表进行查询 ...

  4. SQL 递归查询(根据指定的节点向上获取所有父节点,向下获取所有子节点)

    --------------------01.向上查找所有父节点-----------------WITH TEMP AS (SELECT * FROM CO_Department WHERE ID= ...

  5. Java异常处理和设计【转】

    Java异常处理和设计 在程序设计中,进行异常处理是非常关键和重要的一部分.一个程序的异常处理框架的好坏直接影响到整个项目的代码质量以及后期维护成本和难度.试想一下,如果一个项目从头到尾没有考虑过异常 ...

  6. nyoj 237 游戏高手的烦恼 二分匹配--最小点覆盖

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=237 二分匹配--最小点覆盖模板题 Tips:用邻接矩阵超时,用数组模拟邻接表WA,暂时只 ...

  7. hdu 2199 Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  8. 迅为4412开发板Linux驱动教程之GPIO的初始化

    视频下载地址:http://pan.baidu.com/s/1c06oiti GPIO的初始化 • 在内核源码目录下使用命令“ls drivers/gpio/*.o”,可以看到“gpio-exynos ...

  9. CORDIC原理与FPGA实现(1)

    CORDIC算法的来历与用途大家网上随处可以见到,这里写 一下自己的理解. 将P(x,y)旋转角度a得到新的坐标P’(x’,y’).这里的坐标变换为: x’= x cos(a) – y sin(a)  ...

  10. Chrome浏览器与常用插件推荐

    Chrome浏览器与常用插件推荐 官方chrome下载:http://www.google.cn/chrome/ 提示:需要FQ才能安装. 1,AdBlock 谷歌屏蔽广告: https://chro ...