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. class Pagination(object)分页源码

    class Pagination(object): def init(self, current_page, all_count, per_page_num=10, pager_count=11): ...

  2. 记录一下使用element ui使用级联选择器的坑,级联选择器的默认选中

    Cascader 级联选择器 使用级联选择器我使用的是默认选中值 下面是我的数据格式,只是形式相同,值不同, 后台的数据是这样的不是ID //级联选择器 <el-cascader :props= ...

  3. 【Selenium-WebDriver实战篇】Eclipse上的maven使用JsonObject时,添加依赖后无法自动下载相应的jar包

    ==================================================================================================== ...

  4. Java 内部类和Lambda

    Java内部类 内部类又称为嵌套类,是在类中定义另外一个类.内部类可以处于方法内/外,内部类的成员变量/方法名可以和外部类的相同.内部类编译后会成为完全不同的两个类,分别为outer.class和ou ...

  5. Node.js是什么?提供了哪些内容?

    什么是Node.js? Node.js是基于Chrome V8 引擎的 JavaScript运行时(运行环境). Node.js提供了哪些内容? Node.js运行时,JavaScript代码运行时的 ...

  6. 过滤器的API

    1.API a.生命周期(和servletcontext相似): (1)创建:服务器启动的时候创建(执行init方法). (2)销毁:服务器关闭的时候销毁(执行destory方法). b.filter ...

  7. Xamarin 自定义OnKeyDown 再按一次退出程序的实现

    private DateTime? lastBackKeyDownTime; public override bool OnKeyDown(Keycode keyCode, KeyEvent e) { ...

  8. PowerDesigner 创建表的时候 没有自增长Id的设置项

    今天早上同事创建表的时候,在那个界面没有自增长Id的选项,当时我也纳闷,软件肯定都是一样的,设置的步骤都一样(有些配置好的 我就没改过 然后就忘了还改过些什么步骤了),结果还是没有那个选项 百度了一下 ...

  9. Bzoj 1927: [Sdoi2010]星际竞速(网络流)

    1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MB Description 10年一度的银河系赛车大赛又要开始了.作为全银河最盛大 ...

  10. Java基础教程--安卓入门教程(七)

    关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 欢迎大家关注我的微信公众号:「醉翁猫咪」 什么是接口? 接口的基本语法 接口的基本语法(一) 使用interface定义 接口当中 ...