[LeetCode#250] Count Univalue Subtrees
Problem:
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
.
Analysis:
This problem is super simple.
But, because my poor mastery of Java language, I have committed following simple mistakes. 1. use bit wise operator as and operator.
is_unival & = (root.val == root.left.val);
Line 26: error: illegal start of expression 2. Did not consider Java would automatically optimize the running of code.
For logic operator : and ("&&")
In fact:
C = A && B
If A is false, Java would not execute B. (Thus B must be a primitive value!!! Rather than any function call).
----------------------------------------------------------------------------------------------------------
private boolean helper(TreeNode root, List<Integer> ret) {
if (root == null)
return true;
boolean is_unival = true;
if (root.left != null)
is_unival = is_unival && (root.val == root.left.val);
if (root.right != null)
is_unival = is_unival && (root.val == root.right.val);
is_unival = is_unival && helper(root.left, ret);
is_unival = is_unival && helper(root.right, ret);
if (is_unival)
ret.set(0, ret.get(0)+1);
return is_unival;
}
----------------------------------------------------------------------------------------------------------
Error output:
Input:
[5,1,5,5,5,null,5]
Output:
0
Expected:
4 If is_unival is false before reach
is_unival = is_unival && helper(root.left, ret);
is_unival = is_unival && helper(root.right, ret); Both "helper(root.left, ret)" and "helper(root.right, ret)", would not be executed.
So as to write code as
flag = (helper(root.left, ret) && helper(root.right, ret)).
The above code would cause the problem. The way to avoid such case is to write each statement seprately and use flag_n to hold the temp result. boolean flag1 = true, flag2 = true, flag3 = true, flag4 = true;
if (root.left != null)
flag1 = (root.val == root.left.val);
if (root.right != null)
flag2 = (root.val == root.right.val);
flag3 = helper(root.left, ret);
flag4 = helper(root.right, ret);
is_unival = flag1 && flag2 && flag3 && flag4; Note: For conjunction equation, the initial value for each element should be false rather than true.
Solution:
public class Solution {
public int countUnivalSubtrees(TreeNode root) {
if (root == null)
return 0;
List<Integer> ret = new ArrayList<Integer> ();
ret.add(0);
helper(root, ret);
return ret.get(0);
} private boolean helper(TreeNode root, List<Integer> ret) {
if (root == null)
return true;
boolean is_unival = true;
boolean flag1 = true, flag2 = true, flag3 = true, flag4 = true;
if (root.left != null)
flag1 = (root.val == root.left.val);
if (root.right != null)
flag2 = (root.val == root.right.val);
flag3 = helper(root.left, ret);
flag4 = helper(root.right, ret);
is_unival = flag1 && flag2 && flag3 && flag4;
if (is_unival)
ret.set(0, ret.get(0)+1);
return is_unival;
}
}
[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统计节点值相同的子树
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 ...
- [LeetCode] Count Univalue Subtrees 计数相同值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [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-Count Univalue Subtrees
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
随机推荐
- 开源控件ViewPagerIndicator学习
导航条指示器.ViewPagerIndicator 地址 https://github.com/JakeWharton/ViewPagerIndicator Style是重用控件的一种技术.类似CSS ...
- 抓取Bing每日图片作为网站首页背景
把Bing搜索的背景图片设置为自己网站的背景,实现背景及资讯的每日更新 效果图如下: 理一下思路,首先我们要抓取Bing的每日图片及最新资讯,然后保存图片及信息到本地,最后显示图片及资讯到网站首页. ...
- ios专题 - 图片(UIImage)获取方法
说到图片获取的方法,就得看API文档. UIImage生成实例的方法有: 1)imageNamed 从指定文件返回对象. 这个方法有个比较特殊的地方:该方法首先从系统缓存中寻找该图片,如果有,则从缓存 ...
- (转)iOS中3种正则表达式的使用与比较
.利用NSPredicate(谓词)匹配 例如匹配有效邮箱: NSString *email = @“nijino_saki@.com”: NSString *regex = @"[A-Z0 ...
- foreach的一点理解
首先什么样的数据才能实现foreach 1 实现IEnumerable这个接口 2 有GetEnumerable()这个方法 然后为啥实现这个接口或者有这个方法就可以实现foreach遍历 首先我先用 ...
- Linux网络设置高级指南
from:http://www.oschina.net/question/23734_117144 Linux网络设置高级指南 本文面向的是被Linux复杂的有线无线网络架构弄得头昏脑胀:或者被网上半 ...
- Licode Install CentOS 7.0
mysqldump --add-drop-table -uroot -p123456 jit >/extra_disk/jit.sql sudo npm install -g node-gyp ...
- MySQL中EXPLAIN解释命令详解
MySQL中的explain命令显示了mysql如何使用索引来处理select语句以及连接表.explain显示的信息可以帮助选择更好的索引和写出更优化的查询语句. 1.EXPLAIN的使用方法:在s ...
- (转载)在状态栏即时显示Hint
在状态栏即时显示Hint(所有窗口,包括子窗口控件的hint) 在主窗体中放入一个ApplicationEvents控件.添加如下代码.其中Items[***]中的***代表状态栏的各个子panel, ...
- 用vld分析C++内存泄漏
最近发现项目里有内存泄漏,到网上搜了一些资料,决定用vld来分析解决这个问题. 直接在vs里的"工具和扩展"中搜索vld,然后下载安装就可以了. 我下载下来的是个exe文件,安装在 ...