[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 ...
随机推荐
- UML 中关系图的解说
最近在教软件工程项目实践,就又仔细了解了下UML中各种关系的意义,虽然有点简单,但是有些概念还是经常被混淆的,写在这里是为了加深印象. 关系列表: 继承关系(Generalization): 实现关系 ...
- MySQL安装、基本账户安全(5.0以后版本)
博文目录: 1.Mysql-5.0.40.tar.gz Mysql-5.1.72.tar.gz 2.Mysql-5.5.22.tar.gz 3.Mysql-5.5.34.tar.gz 4.Mysql- ...
- tomcat原理分析与简单实现
tomcat原理分析与简单实现 https://blog.csdn.net/u014795347/article/details/52328221 2016年08月26日 14:48:18 卫卫羊习习 ...
- 收集的一些python基础教学博客
=======python3学习链接======= Python 3 教程:http://www.runoob.com/python3/python3-tutorial.html 深入python3: ...
- Linux运维面试贩卖思路如下
1.自我介绍 2.技术介绍 3.上家公司情况介绍.多少人的团队.运维多少人.多少设备.公司什么业务.访问量多少.并发多少.架构多大,然后介绍公司架构.CDN->负载均衡->web-> ...
- 浏览器。浏览器对象检测、Chrome调试工具
chrome浏览器的flash问题: 2017-12-26 chrome浏览器的flash有无法显示无法正常运行的问题时,解决方法如下: https://qzonestyle.gtimg.cn/qzo ...
- java垃圾回收几种算法
1.引用计数法 2.标记——清除法 3.标记——整理算法 4.copying算法 5.generation算法(新生代.老年代.持久代) 详情参考:深入理解 Java 垃圾回收机制
- Delphi 变体数组 Dataset Locate 查找定位
Format 函数 Delphi 支持“开参数”和动态数组,变体数组,使用时的语法类似 Delphi 中的集合:采用两个方括号把不同类型的变量括起来(这太方便了啊),也可以采用声明一个 TVarRec ...
- xe Style
//注意引用:vcl.themes, vcl.styles, IOutils procedure TForm1.FormCreate(Sender: TObject); var stylename: ...
- 批量得到/修改word超链接
Alt+F9或者勾选下面 此时的超链接地址全部转换为文本形式进行显示; 然后可以用全局替换搜索来处理