[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 ...
随机推荐
- 用PNG作为Texture创建Material
转自:http://aigo.iteye.com/blog/2279512 1,导入一张png素材作为Texture 2,新建一个Material,设置Blend Mode为Translucent,连 ...
- Android ffmpeg rtmp(source code)
souce code: Android.mk 编译生成APK需要调用的so文件 LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODU ...
- Spark分析之MemoryStore
private case class MemoryEntry(value: Any, size: Long, deserialized: Boolean) class MemoryStore(bloc ...
- 使用nproxy代理本地服务到内网
前端开发中:很多场景需要在局域网下的其他手机或设备查看网页, 问题来了, web服务部署在本机的某个端口上(8080),只能通过本机浏览器访问, 怎样能让局域网下的其他设备也访问呢?可能你会说 关闭 ...
- UVA540-队列
题意: 每一个数字有自己所属的团队,如果所属的团队已经有人在队列里,放在团队的最后一个,要不然放队列里最后一个 注意:一个团队里的最多1000个元素,但是入队,出队的操作会达到200000次 解法:循 ...
- hive 锁
HiveQL是一种SQL语言,但缺少udpate和insert类型操作时的行,列或者查询级别的锁支持,hadoop文件通常是一次写入(支持有限的文件追加功能),hadoop和hive都是多用户系统,锁 ...
- XCode iOS Simulator 模拟器
XCode7.3下,默认带了iOS 9.3 Simulator,iOS 8.4 Simulator总是安装不成功. mac os X,里的模拟器,全屏 ,windows win键+1/2/3 切换全屏 ...
- J2SE 8的集合
List ArrayList查询效率高LinkedList插入删除效率高 ArrayList ArrayList<String> arrayList = new ArrayList< ...
- 可视化库-seaborn-单变量绘图(第五天)
1. sns.distplot 画直方图 import numpy as np import pandas as pd from scipy import stats, integrate impor ...
- android MD5 SHA1
参考文章: AndroidStudio 中怎样查看获取MD5和SHA1值(应用签名)(https://www.cnblogs.com/zhchoutai/p/7102516.html) 使用 java ...