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. [windows]禁止指定用户使用远程桌面服务登录

    windows2003下禁止用户远程登录的方法如下: 1.打开控制面板 > 管理工具 > 本地安全策略 2.安全策略-->本地策略-->用户权限分配-->通过终端服务拒绝 ...

  2. 删除CListCtrl中具有某一相同数据的所有行

    删除CListCtrl中具有某一相同数据的所有行 ================================== 本文原创,转载请注明出处:http://blog.csdn.net/wlsgzl ...

  3. asp.net的心得体会

    1,gridview有点强大 2,页面的冗余代码有点多,性能略差. 3,access数据库的使用. 4,C/S端的开发,接触winform的开发 我自认为asp.net开发就像在一个别人限制好的画布上 ...

  4. Swing应用开发实战系列之二:设计日期选择面板窗口

    Swing本身没有提供什么华丽丽的日期时间选择控件,所以笔者就在网上搜了个第三方的jar包jdatepicker-1.3.2.jar,基于此设计了个很轻量的日期选择面板,很简单的.效果图如下所示: 代 ...

  5. Ajax详解及其案例分析------如何获得Ajax对象,使用Ajax对象发送GET和POST请求,校验用户名,POST和GET请求时的乱码处理,实现级联的下拉列表

    本节主要内容预览: 1 获得Ajax对象 2 使用Ajax对象发送GET请求 3 使用Ajax对象发送POST请求 4 使用Ajax校验用户名 5 POST请求时的乱码处理 6 GET请求时的乱码处理 ...

  6. 内存流和null字节

    #include <stdio.h> #include <string.h> int main() { ]={}; FILE* fp = fmemopen(buf,," ...

  7. [转]比NPOI更討喜的Excel元件-EPPlus

    本文转自:http://blog.darkthread.net/post-2012-05-12-epplus.aspx 前陣子發表 [潛盾機]將檔案結構匯成Excel文件,從網友佑翔的留言(特此感謝) ...

  8. Unity3D 实现简单的语音聊天 [iOS版本]

    现在很多手机游戏中的聊天系统都加入语音聊天的功能,相比于传统的文字聊天,语音聊天在MMORPG中显得尤为重要,毕竟直接口头交流总比你码字快得多了,也更直观些. 实现语音聊天的方法很多,U3D中有不少第 ...

  9. 计算机网络: IP地址,子网掩码,默认网关,DNS服务器详解

    楔子: 以Windows系统中IP地址设置界面为参考(如图1), IP地址, 子网掩码, 默认网关 和 DNS服务器, 这些都是什么意思呢? 学习IP地址的相关知识时还会遇到网络地址,广播地址,子网等 ...

  10. UESTC 883 方老师与两个串 --二分搜索+DP

    CF原题 由题可知,n,m太大,无法开出dp[n][m]的数组. 观察发现s/e最大为300,也就是说,选用第一种操作的次数不会超过300. 于是定义dp[i][j],第一个串的前i个数,使用了j次第 ...