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. [软件] Omnigraffle

    一个商业软件, mac下画画图, 还挺好用的. 网上可以找到可用的注册码 https://blog.csdn.net/glw0223/article/details/90736751

  2. scrapy 在pycharm中调试 不用到命令行中启动爬虫方法

    (目录结构如上图) 在主目录中加入main.py,在其中加入代码,运行此文件就可以运行整个爬虫: # -*- coding: utf-8 -*- __author__='pasaulis' #在程序中 ...

  3. UML-对象设计要迭代和进化式

    1.在<如何面向对象设计>和<如何进行对象设计?>中,对如何迭代和进化式的设计对象做些总结: 1).轻量+简短 2).快速进入编码+测试 3).不要试图在UML中细化所有事物 ...

  4. pywin32获得tkinter的Canvas窗口句柄,并在上面绘图

    上一篇博文获得主窗口句柄使用的是root.frame或者通过子控件调用master.frame方法,但是子控件本身没有frame方法.那么怎么获得子控件的句柄呢?试过了很多办法,想过把Canvas当作 ...

  5. Java 14 令人期待的 5 大新特性,打包工具终于要来了

    随着新的 Java 发布生命周期的到来,新版本预计将于 2020 年 3 月发布,本文将对其中的 5 个主要特性作些概述. Java 13刚刚发布给开发人员使用不久,最新版本的JDK于2019年9月发 ...

  6. C#——反射,自动生成添加的SQL语句

    C#中的反射.是C#中特别重要也是特别神奇的特性,对后面学习框架,了解框架的原理.以及自己写框架,都是必不可少的.学习反射的过程中.总给我一种茅塞顿开的感觉,以前不懂的,现在懂了 反射的介绍:http ...

  7. JZOJ-TG817-A-solution

    T1 考虑是否有一种排序方法使得最优解都相邻,这种排序方法就是按照过一个点x的斜率为(P/Q)的直线的截距 排序之后考虑临项即可,O(N) T2 exit

  8. Ubuntu---gedit 打开windows 下 .txt 文件乱码的解决方法

    问题出现情况:在windows 下编辑的 .txt 文件复制到 Ubuntu 下打开,默认打开方式为 gedit 软件打开,出现如下乱码: 出现原因:在 windows 系统下,.txt 文件默认编码 ...

  9. NGDC|BIGD

    生命组学 生命起源经过复杂演化诞生了大量生物体及其基因组. 现今NCBI最大的基因组: 植物:糖松27.6G 动物:墨西哥蝾螈32.4G 大数据能做什么? 大数据时代如同大航海时代一样,需要具有与时代 ...

  10. JSONP 跨域问题

    JSONP跨域请求   什么是跨域: 1.域名不同 2.域名相同端口不同 js出于对安全考虑不支持跨域请求.我们可以使用JSONP解决跨域问题. 一.JSONP是什么 JSONP(JSON with ...