[leetcode]250. 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.
Example :
Input: root = [5,1,5,5,5,null,5] 5
/ \
1 5
/ \ \
5 5 5 Output: 4
题意:
给定一棵二叉树,求所有节点都同值的二叉树。
思路:
dfs
代码:
class Solution {
int result;
public int countUnivalSubtrees(TreeNode root) {
result = 0;
helper(root);
return result;
} private boolean helper(TreeNode node){
if(node == null ) return true; boolean left = helper(node.left);
boolean right = helper(node.right); if(left&&right){
if ((node.left!= null && node.val != node.left.val) || (node.right!= null && node.val != node.right.val) ){
return false;
}
result++;
return true;
}
return false;
}
}
[leetcode]250. Count Univalue Subtrees统计节点值相同的子树的更多相关文章
- [LeetCode] 250. Count Univalue Subtrees 计算唯一值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [LeetCode#250] Count Univalue Subtrees
Problem: Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all ...
- [LeetCode] Count Univalue Subtrees 计数相同值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- 250. Count Univalue Subtrees
题目: Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes ...
- [LC] 250. Count Univalue Subtrees
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [Locked] Count Univalue Subtrees
Count Univalue Subtrees Given a binary tree, count the number of uni-value subtrees. A Uni-value sub ...
- [Swift]LeetCode250.计数相同值子树的个数 $ Count Univalue Subtrees
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [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 ...
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
随机推荐
- django中视图处理请求方式(FBV、CBV)
FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class base v ...
- 了解ES6
内容: 1.ES6介绍及基础 2.模块.类和继承 3.ES6高级特性 4.Generator和Iterator 5.异步编程 6.函数相关 内容参考:<ES6 标准入门> ES6标准阅读链 ...
- 暴搜 - Codeforces Round #327 (Div. 2) E. Three States
E. Three States Problem's Link Mean: 在一个N*M的方格内,有五种字符:'1','2','3','.','#'. 现在要你在'.'的地方修路,使得至少存在一个块'1 ...
- linux运维工程师工作中的一些常见问题解决方法
http://blog.sina.com.cn/s/blog_b9fe247a0101anoe.html 1.shell脚本死活不执行 问题:某天研发某同事找我说帮他看看他写的shell脚本,死活不执 ...
- CSS3d 基础
-webkit-transform-style:-webkit-preserve-3d;//设置3D转换 translateX:px; 平移 translateY:px; translateZ:px; ...
- java web 读取文件,文件路劲不对的问题
都知道,一般java项目,编译后的文件是在classes文件夹下面: 而java web项目,则是在WEB-INF/classes文件夹下面.new File(fileName)须先获取tomcat中 ...
- Mongodb 安装 和 启动
教程:http://www.mongodb.org.cn/tutorial/59.html 下载 >wget https://fastdl.mongodb.org/linux/mongodb-l ...
- as3 Function 中的call与apply方法
apply方法,作用跟call一样,也可以用来改变函数执行时,this指针的指向,区别在于apply方法要求第二个参数必须是数组形式 例子,舞台上添加名为a和b的实例mc 案例1: a.getNumV ...
- springBoot异常处理
1.status=404 Whitelabel Error Page Whitelabel Error Page This application has no explicit mapping fo ...
- ABAP-关于COMMIT WORK 和COMMIT WORK AND WAIT
转载:https://blog.csdn.net/champaignwolf/article/details/6925019 首先说明一点:更新是异步的,更新是由SAP中UPD1和UPD2两个进程执行 ...