[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 ...
随机推荐
- [UE4]ue4 FString 中文乱码问题
使用FString出现乱码,最简单的情况,FString Str = "你好"; 这时候就会出现乱码,解决方法是改成这样 FString Str = TEXT("你好&q ...
- Windows7下搭建Eclipse+Python开发环境
机器: Windows7_x86_64 前提: 机器已成功安装Python2.7,并配置好环境变量. 步骤: 一.Eclipse的安装 Eclipse是基于java的一个应用程序,因此需要一个java ...
- js && Jquery 的回车事件
有时候我们需要捕获页面上的回车事件,以达到一些特殊效果,例如在登录页面用户输入完登录名和密码后习惯直接敲回车,这时需要捕获回车事件,在回车事件中激活form元素 1.纯Java Script版 首先要 ...
- Hibernate 和 MyBatis 的对比
一.开发对比 开发速度 Hibernate 的真正掌握要比MyBatis来的难些.MyBatis框架较轻量级,相对简单很容易上手,但也相对简陋些.个人觉得要用好 MyBatis 还是要首先理解好 Hi ...
- mysql5.6修改字符编码,ERR:Illegal mix of collations for operation 'concat'
mysql5.6修改字符编码,ERR:Illegal mix of collations for operation 'concat' 1.问题起因:搭建环境初始化mysql的时候看到mysql配置文 ...
- ORM介绍(字段 和 字段的参数)
ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用描述 ...
- [Python] 牛顿插值
插值公式为: 差商递归公式为: # -*- coding: utf-8 -*- #Program 0.4 Newton Interpolation import numpy as np import ...
- javascript 常用获取页面宽高信息 API
在页面的构建中 常常会需要获取页面的一些宽高信息,例如实现 惰性加载图片 需要获取页面的可见区域高度 和 已滚动区域的高度,以判断图片所在位置是否可见来决定加载图片的时间, 花点时间整理了一下,获取页 ...
- 配置Ubuntu虚拟环境
1.ubuntu默认root用户没有激活,激活root用户,就要为root用户创建密码 $sudo passwd root 2.修改主机名 $vi /etc/hostname 3.安装ssh服 ...
- zabbix监控系统日志
监控日志必须让agent运行在主动模式 参考网站:https://www.cnblogs.com/dadonggg/p/8611054.html?from=singlemessage