原题链接在这里:https://leetcode.com/problems/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.

题解:

bottom-up recursion. dfs返回当前root下是不是univalue tree.

Time Complexity: O(n).

Space: O(logn). height of tree.

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     public int countUnivalSubtrees(TreeNode root) {
12         int [] res = {0};
13         dfs(root, res);
14         return res[0];
15     }
16
17     private boolean dfs(TreeNode root, int [] res){
18         if(root == null){
19             return true;
20         }
21
22         boolean left = dfs(root.left, res);
23         boolean right = dfs(root.right, res);
24         if(left && right){
25             if(root.left!=null && root.left.val!=root.val){
26                 return false;
27             }
28
29             if(root.right!=null && root.right.val!=root.val){
30                 return false;
31             }
32
33             res[0]++;
34             return true;
35         }
36
37         return false;
38     }
39 }

类似Longest Univalue Path.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/5187437.html

LeetCode 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 of ...

  2. LeetCode 38. Count and Say

    问题链接 LeetCode 38. Count and Say 题目解析 找规律,每一个数字串是上一个数字串的"读法".比如:n=1时为"1",读作" ...

  3. 【同124】LeetCode 687. Longest Univalue Path

    LeetCode 687. Longest Univalue Path Solution1: 参考链接:http://zxi.mytechroad.com/blog/tree/leetcode-687 ...

  4. 【Hard 递归 动态规划 回文串15】LeetCode 730. Count Different Palindromic Subsequences

    LeetCode 730. Count Different Palindromic Subsequences 博客转载自:http://zxi.mytechroad.com/blog/dynamic- ...

  5. [勇者闯LeetCode] 38. Count and Say

    [勇者闯LeetCode] 38. Count and Say Description The count-and-say sequence is the sequence of integers b ...

  6. leetcode 1925. Count Square Sum Triples(python)

    描述 A square triple (a,b,c) is a triple where a, b, and c are integers and a^2 + b^2 = c^2. Given an ...

  7. LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题

    题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...

  8. leetcode -- 357. Count Numbers with Unique Digits

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  9. LeetCode 1885. Count Pairs in Two Arrays(二分查找)

    文章目录 1. 题目 2. 解题 1. 题目 Given two integer arrays nums1 and nums2 of length n, count the pairs of indi ...

最新文章

  1. 浅谈自然语言处理技术在自动化的应用
  2. 通过python实现超市购物系统(通过列表简单实现版)
  3. 牛客第六场 H-Hopping Rabbit
  4. U-Boot 之四 构建过程(Kconfig 配置 + Kbuild 编译)详解
  5. 数据分析与挖掘-python常用数据预处理函数
  6. Linux 应用程序 网络通讯函数记录
  7. Android(java)学习笔记155:中文乱码的问题处理(qq登录案例)
  8. Design Patterns
  9. servlet之监听器
  10. PDMS Pipeline Tool 教程(五):汇料属性设置
  11. 【SELinux】vendor_file_contexts没有被编译到vendor/etc/selinux/路径下
  12. photoshop(ps)基础入门知识 认识图层
  13. WordPress美女图集COS写真整站自适应源码带完整数据
  14. 1109 Group Photo (25分)/1055 集体照 (25分)后三个测试点
  15. php c端,蛋白测序(N端,C端测序)
  16. 教育孩子,是从小的润雨细无声。纯属个人文学闷骚型。。。
  17. 劳务员培训建筑八大员培训劳务员建筑劳务分包管理突出的问题
  18. 文本分类的14种算法(1)
  19. element的table组件,表头合并(合并表头单元格)
  20. flink-cdc 使用

热门文章

  1. 汇编之loop指令使用栈实现二重循环,同时了解汇编函数(过程)的概念用法
  2. 机器学习资料整理,收藏了不后悔!
  3. #1415 : 后缀数组三·重复旋律3 (最长公共子串)
  4. python 利用pandas库实现 读写 .csv文件
  5. linux w 命令参数解释
  6. mysql错误号码1040_Mysql ERROR 1040 (00000): Too many connections
  7. 并查集(加权规则、折叠规则)
  8. makefile与make
  9. 莫烦python简历_强化学习传说:第一章 模仿学习
  10. AKKA文档(java版)——准备开始