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

题意:

给定一棵二叉树,求所有节点都同值的二叉树。

思路:

dfs

代码:

 class Solution {
int result;
public int countUnivalSubtrees(TreeNode root) {
result = 0;
helper(root);
return result;
} private boolean helper(TreeNode node){
if(node == null ) return true; boolean left = helper(node.left);
boolean right = helper(node.right); if(left&&right){
if ((node.left!= null && node.val != node.left.val) || (node.right!= null && node.val != node.right.val) ){
return false;
}
result++;
return true;
}
return false;
}
}

[leetcode]250. Count Univalue Subtrees统计节点值相同的子树的更多相关文章

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

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

  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] Count Univalue Subtrees 计数相同值子树的个数

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

  4. 250. Count Univalue Subtrees

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

  5. [LC] 250. Count Univalue Subtrees

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

  6. [Locked] Count Univalue Subtrees

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

  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] 687. Longest Univalue Path 最长唯一值路径

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  9. [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...

随机推荐

  1. 【BZOJ】1257: [CQOI2007]余数之和(除法分块)

    题目 传送门:QWQ 分析 大佬和我说本题是除法分块,莫比乌斯反演中也有用到. QwQ我不会莫比乌斯反演啊~ 题目让我们求  $ \sum_{i=1}^n  k\mod n $ 然后根据$ a \mo ...

  2. 初步认识AutoMapper

      AutoMapper 初步认识AutoMapper 前言 手动映射 使用AutoMapper 创建映射 Conventions 映射到一个已存在的实例对象   前言 通常在一个应用程序中,我们开发 ...

  3. ZooKeeper系列(1) 整体介绍(转)

    原文地址:https://www.cnblogs.com/wuxl360/p/5817471.html 一.分布式协调技术 在给大家介绍ZooKeeper之前先来给大家介绍一种技术——分布式协调技术. ...

  4. ORM PetaPoco 框架的 CRUD 操作

    PetaPoco 的查询操作 public IEnumerable<T> GetAll(string sqlString, object[] obj) { try { IEnumerabl ...

  5. java内存模型(一)正确使用 Volatile 变量

    文章转载自: 正确使用 Volatile 变量   Java 语言中的 volatile 变量可以被看作是一种 "程度较轻的 synchronized":与 synchronize ...

  6. mybatis匹配字符串的坑

    where语句中我们经常会做一些字符串的判断,当传入的字符串参数为纯数字时,在mybatis的条件语句test里匹配全数字字符串需要注意会有如下现象: 所以里面的字符串需要加单引号,mybatis是匹 ...

  7. 2018-2019-2 《网络对抗技术》Exp6 信息搜集与漏洞扫描 Week9 20165233

    Exp6 信息搜集与漏洞扫描 目录 一.基础问题 二.实验步骤 实验点一:各种搜索技巧的应用 实验点二:DNS IP注册信息的查询 实验点三:基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具 ...

  8. docker 配置pull源

    登录阿里云 找到镜像服务 在/etc/docker下创建daemon.json文件并写入 最后重启docker服务 sudo service docekr restart

  9. c++官方文档-模版函数和重载

    #include<stdio.h> #include<iostream> #include<queue> #include<map> #include& ...

  10. 0_Simple__UnifiedMemoryStreams

    使用 OpenMP 和 pthreads 两种环境,利用实现统一内存编址,计算基本的矩阵乘法 result = α * A * x + β * result . ▶ 源代码 #include < ...