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. SMBus PEC

    SMBus一种I2C总线的变种 SMBus 提供了PEC方式,提高了传输的可靠性. 总线的发展都是在提高速度,提高可靠性或者提高传输效率上下功夫. PEC不具备纠错的能力,是在I2C link lay ...

  2. selenium常用的API(七)判断元素是否可见

    web页面不可见的元素虽不在页面上显示,但是存在于DOM树中,这些元素webdriver也能找到. element.is_displayed()方法可以判断元素是否在页面上显示,如果显示返回True, ...

  3. re正则match、search、findall、finditer函数方法使用

    match 匹配string 开头,成功返回Match object, 失败返回None,只匹配一个. search 在string中进行搜索,成功返回Match object, 失败返回None, ...

  4. Oracle中INSTR函数与SQL Server中CHARINDEX函数

    Oracle中INSTR函数与SQL Server中CHARINDEX函数 1.ORACLE中的INSTR INSTR函数格式:INSTR(源字符串, 目标字符串, 起始位置, 匹配序号) 说明:返回 ...

  5. Uva11762 Race to 1——有向无环图&&记忆化搜索

    题意 给出一个整数 $N$,每次可以在不超过 $N$ 的素数中等概率随机选择一个 $P$,如果 $P$ 是 $N$ 的约数,则把 $N$ 变成 $N/P$,否则 $N$ 不变.问平均情况下需要多少次随 ...

  6. PHP-FPM config 文件生产环境

    ;;;;;;;;;;;;;;;;;; ; Global Options ; ;;;;;;;;;;;;;;;;;; [global] pid = run/php-fpm.pid error_log = ...

  7. [golang]Go常见问题:# command-line-arguments: ***: undefined: ***

    今天遇见一个很蛋疼的问题,不知道是不是我配置的问题,IDE直接run就报错. 问题描述 在开发代码过程中,经常会因为逻辑处理而对代码进行分类,放进不同的文件里面:像这样,同一个包下的两个文件,点击id ...

  8. hdu1501 Zipper[简单DP]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu1501 题干 代码和解释 最优子结构分析:设这三个字符串分别为a.b.c,如果a.b可以组成c,那么c的最后一个字母必定来自a或者b的最后一个 ...

  9. 20189220 余超《Linux内核原理与分析》第八周作业

    Linux内核如何装载和启动一个可执行程序 本章知识点 ELF(Executable and Linking Format)是一种对象文件的格式,用于定义不同类型的对象文件(Object files) ...

  10. 20189220 余超《Linux内核原理与分析》第四周作业

    构造一个简单的Linux系统MenuOS 第三章基础知识 计算机的三大法宝:存储计算机,函数调用堆栈,中断. 操作系统的两把宝剑:中断上下文,进程上下文. Linux内核源码的目录结构: arch目录 ...