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.

给一个二叉树,求唯一值子树的个数。唯一值子树的所有节点具有相同值。

解法:递归

Java:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
int count = 0; public int countUnivalSubtrees(TreeNode root) {
if (root == null) return 0;
isUnival(root);
return count;
} private boolean isUnival(TreeNode root) {
if (root == null) return true;
if (isUnival(root.left) & isUnival(root.right)) {
if (root.left != null && root.left.val != root.val) return false;
if (root.right != null && root.right.val != root.val) return false;
count++;
return true;
}
return false;
}
}  

Java:

public class Solution {
public int countUnivalSubtrees(TreeNode root) {
int[] count = new int[] {0};
isUnivalSubtrees(root,count);
return count[0];
} private boolean isUnivalSubtrees(TreeNode root, int[] count) {
if(root == null) return true; boolean left = isUnivalSubtrees(root.left, count);
boolean right = isUnivalSubtrees(root.right, count);
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;
}
count[0]++;
return true;
}
return false;
}
}  

Python:

# Time:  O(n)
# Space: O(h)
class Solution(object):
# @param {TreeNode} root
# @return {integer}
def countUnivalSubtrees(self, root):
[is_uni, count] = self.isUnivalSubtrees(root, 0);
return count; def isUnivalSubtrees(self, root, count):
if not root:
return [True, count] [left, count] = self.isUnivalSubtrees(root.left, count)
[right, count] = self.isUnivalSubtrees(root.right, count)
if self.isSame(root, root.left, left) and \
self.isSame(root, root.right, right):
count += 1
return [True, count] return [False, count] def isSame(self, root, child, is_uni):
return not child or (is_uni and root.val == child.val)

C++:

// Time:  O(n)
// Space: O(h) /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int countUnivalSubtrees(TreeNode* root) {
int count = 0;
isUnivalSubtrees(root, &count);
return count;
} bool isUnivalSubtrees(TreeNode* root, int *count) {
if (root == nullptr) {
return true;
}
bool left = isUnivalSubtrees(root->left, count);
bool right = isUnivalSubtrees(root->right, count);
if (isSame(root, root->left, left) &&
isSame(root, root->right, right)) {
++(*count);
return true;
}
return false;
} bool isSame(TreeNode* root, TreeNode* child, bool is_uni) {
return child == nullptr || (is_uni && root->val == child->val);
}
};

  

类似题目:

[LeetCode] 687. Longest Univalue Path 最长唯一值路径

All LeetCode Questions List 题目汇总

[LeetCode] 250. Count Univalue Subtrees 计算唯一值子树的个数的更多相关文章

  1. [LeetCode] 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统计节点值相同的子树

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

  3. [LeetCode#250] Count Univalue Subtrees

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

  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] 222. Count Complete Tree Nodes 求完全二叉树的节点个数

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

  9. [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 ...

随机推荐

  1. Caused by SSLError("Can’t connect to HTTPS URL because the SSL module is not available)

    window7系统: 今天刚安装的anaconda(开源的Python包管理器),把原来的python3和python2都给卸载了,结果运行爬虫程序的时候报错: Caused by SSLError( ...

  2. Centos7-重建官方yum源

    删除yum源,重建官方 cd /etc/yum.repos.d/ #删除所有 rpm -Uvh --force http://mirror.centos.org/centos-7/7.7.1908/o ...

  3. 《exception》第九次团队作业:Beta冲刺与验收准备(第一天)

    一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件黑盒测试技术:2.学会编制软件项目 ...

  4. python SQLAlchemy的简单配置和查询

    背景: 今天小鱼从0开始配置了下 SQLAlchemy 的连接方式,并查询到了结果,记录下来 需要操作四个地方 1. config  ------数据库地址 2.init ----- 数据库初始化 3 ...

  5. 实用Golang库

    框架: 1. Golang轻量级并发服务器框架: zinx / https://www.jianshu.com/p/23d07c0a28e52. 国内谢大牛模仿django制作的重框架: beego3 ...

  6. 实现:调用API函数ShowWindow()来隐藏窗口

    只需要将相应代码复制即可. 代码如下: #include <iostream> #include <windows.h> int main() { HWND hDos; //声 ...

  7. Optimal Marks SPOJ - OPTM(最小割)

    传送门 论文<最小割模型在信息学竞赛中的应用>原题 二进制不同位上互不影响,那么就按位跑网络流 每一位上,确定的点值为1的与S连一条容量为INF的有向边.为0的与T连一条容量为INF的有向 ...

  8. Centos7配置静态网卡

    1.打开VMware,查看ifconfig 2.进入网卡编辑 [root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhos ...

  9. 【BZOJ4237】 稻草人 CDQ分治+单调栈

    ## 题目描述 JOI村有一片荒地,上面竖着N个稻草人,村民们每年多次在稻草人们的周围举行祭典. 有一次,JOI村的村长听到了稻草人们的启示,计划在荒地中开垦一片田地.和启示中的一样,田地需要满足以下 ...

  10. C++ EH Exception(0xe06d7363)----抛出过程

    C++ EH Exception是Windows系统VC++里对c++语言的throw的分类和定义,它的代码就是0xe06d7363.在VC++里其本质也是SEH结构化异常机制.在我们分析用户崩溃的例 ...