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 ...
随机推荐
- C语言--static全局使用示例
前言:看到很多使用Objective-C开发IOS的大牛,有时候会使用static全局变量,相比之下,我却很少用这个,从而很少对其有着比较有实质意义的理解,甚至更别说运用它了. 今天,经过一番思考和自 ...
- Cent OS服务器配置(JDK+Tomcat+MySQL)
本文摘自:Cent OS服务器配置(JDK+Tomcat+MySQL) 学习tar解压 解压 tar 文件 tar -zxvf apache-tomcat-6.0.35.tar.gz tomcat ...
- 双系统如何删除Linux,恢复Windows从MBR引导启动?
嗯,现在愿意尝试Linux的人越来越多了.通常,如果一台电脑里已经装有了Windows,再装Linux,安装时,Linux的grub引导程 序就会覆盖掉保存在MBR当中的原来的Windows引导程序. ...
- CTF中那些脑洞大开的编码和加密
0x00 前言 正文开始之前先闲扯几句吧,玩CTF的小伙伴也许会遇到类似这样的问题:表哥,你知道这是什么加密吗?其实CTF中脑洞密码题(非现代加密方式)一般都是各种古典密码的变形,一般出题者会对密文进 ...
- 修复ORACLETNS-12545 因目标主机或对象不存在错误
现象: ORACLE启动不了,输入cmd->lsnrctl后,出现如下错误, 经查资料,发现是主机名可能解析有问题,后来在D:\oracle\ora92\network\admin下打开list ...
- Effective Java 51 Beware the performance of string concatenation
Using the string concatenation operator repeatedly to concatenate n strings requires time quadratic ...
- 最短路径之迪杰斯特拉(Dijkstra)算法
迪杰斯特拉(Dijkstra)算法主要是针对没有负值的有向图,求解其中的单一起点到其他顶点的最短路径算法.本文主要总结迪杰斯特拉(Dijkstra)算法的原理和算法流程,最后通过程序实现在一个带权值的 ...
- oracle树操作(select start with connect by prior)
oracle中的递归查询可以使用:select .. start with .. connect by .. prior 下面将会讲述oracle中树形查询的常用方式,只涉及到一张表. 一. 建表语句 ...
- HttpClent4.3 的例子
package com.unbank.robotspider.util; import java.io.IOException; import java.net.MalformedURLExcepti ...
- 简单谈谈RAID
RAID是“Redundant Array of Independent Disk”的缩写,翻译过来叫做独立磁盘的冗余阵列,其实就是磁盘的存储.访问.备份技术.在谈RAID之前,先简单学习一下存储器的 ...