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.

Example :

Input:  root = [5,1,5,5,5,null,5]

              5
/ \
1 5
/ \ \
5 5 5 Output: 4
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int res = 0;
public int countUnivalSubtrees(TreeNode root) {
helper(root);
return res;
} private boolean helper(TreeNode root) {
if (root == null) {
return true;
}
boolean left = helper(root.left);
boolean right = helper(root.right);
if (left && right) {
if (root.left != null && root.left.val != root.val) {
return false;
}
if (root.right != null && root.right.val != root.val) {
return false;
}
res += 1;
return true;
} /** another way
if (left && right &&
(root.left == null || root.val == root.left.val) &&
(root.right == null || root.val == root.right.val)) {
res++;
return true;
}
*/
return false;
}
}

[LC] 250. Count Univalue Subtrees的更多相关文章

  1. 250. Count Univalue Subtrees

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

  2. [LeetCode#250] Count Univalue Subtrees

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

  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 计算唯一值子树的个数

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

  5. [Locked] Count Univalue Subtrees

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

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

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

  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. LeetCode-Count Univalue Subtrees

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

  9. LC 652. Find Duplicate Subtrees

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

随机推荐

  1. Django2.0——中间件

    Django中间件middleware本质是一个类,在请求到返回的中间,类中不同的方法会在指定的时机中被触发.setting.py的变量MIDDLEWARE_CLASSES中的每一个元素都是中间件,且 ...

  2. Tomcat启动报内存溢出错误:java.lang.OutOfMemoryError: PermGen space

    windows操作系统 找到D:\Tomcat-7\apache-tomcat-7.0.28\bin(解压安装的Tomcat)目录下的catalina.bat文件,打开该文件,找到下图所示的内容:添加 ...

  3. sklearn.metrics中的评估方法介绍(accuracy_score, recall_score, roc_curve, roc_auc_score, confusion_matrix)

    1 accuracy_score:分类准确率分数是指所有分类正确的百分比.分类准确率这一衡量分类器的标准比较容易理解,但是它不能告诉你响应值的潜在分布,并且它也不能告诉你分类器犯错的类型.常常误导初学 ...

  4. 浅谈JVM - 内存结构(二)- 虚拟机栈|凡酷

    2.1 定义 Java Virtual Machine Stacks(Java虚拟机栈) Java 虚拟机栈描述的是 Java 方法执行的内存模型,用于存储栈帧,是线程私有的,生命周期随着线程启动而产 ...

  5. 静态页面缓存(thymeleaf模板writer)

    //前端html <!DOCTYPE html><html lang="en"> <head> <meta charset="U ...

  6. 对于centos的运用ssh远程连接

    1,首先安装ssh服务器 $yum install openssh-server 2,记录你当前centos的ip地址 $ifconfig 3,再在windows里面安装putty 4安装完成后, 在 ...

  7. vim,neovim 配置文件

    插件管理用的是 https://github.com/junegunn/vim-plug 打开网址,会有示例,如何安装它本身,以及一些插件,照做,很容易完成 里面有两个可用的配置文件,cp_vimrc ...

  8. 注册服务和发现服务 Eureka

    来自蚂蚁课堂: 注册服务和发现服务 1.原理如图: 注册中心负载均衡: 实践 注册中心 集群:

  9. WebServerFactoryCustomizer set the port, address, error pages etc.

    package com.ioc; import org.springframework.boot.SpringApplication; import org.springframework.boot. ...

  10. UML-业务规则

    样例: