[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 ...
随机推荐
- C++Primer笔记二
真是一本好书,就这么点,就感觉学到很多了,当然也是我水平太差. 用shell或者bash的时候有一个文件重定向,就是每次程序运行的时候,我们都需要手动输入内容,然后程序输出内容,这时可以用文件来代替. ...
- windows8 8.1 安装完 ubuntu无法挂载 ntfs分区 解决方法
windows8 8.1 安装完 ubuntu无法挂载 ntfs分区 解决方法: 最近安装完发现8.1系统后,ubuntu无法加载以前的ntfs分区了,特别是我添加到了/etc/fstab里面了 导致 ...
- react native ios 开发,基础配置笔记。
一.获取硬件信息,使用react-native-device-info插件,配置说明: 1.首先需要安装组件:npm install react-native-device-info --save 2 ...
- Bzoj 3831 [Poi2014]Little Bird
3831: [Poi2014]Little Bird Time Limit: 20 Sec Memory Limit: 128 MB Submit: 310 Solved: 186 [Submit][ ...
- 【BZOJ3270】【高斯消元】博物馆
Description 有一天Petya和他的朋友Vasya在进行他们众多旅行中的一次旅行,他们决定去参观一座城堡博物馆.这座博物馆有着特别的样式.它包含由m条走廊连接的n间房间,并且满足可以从任何一 ...
- 第3章文件I/O总结
1. open和create函数在fcntl.h中,close.lseek.read.write函数在unistd.h中 open函数通过进程有效用户ID判断读文件的权限 可以调用access函数判断 ...
- notepad++插件使用说明
在进行渗透测试的过程中,经常需要分析一些js或xml文件,或者是一些url,它们多是经过压缩或编码的,看起来不方便.这里介绍几款常用的notepad++插件,会极大的方便我们的开发测试. 1 Xml ...
- 《Thinking In Java第四版》拾遗
<Thinking In Java第四版>拾遗 转自我的github(http://katsurakkkk.github.io/2016/05/Thinking-In-Java%E7%AC ...
- js json和对象互相转换
http://www.jb51.net/article/44562.htm obj = JSON.parse(string) | obj = jQuery.parseJSON(str) 将JSON字符 ...
- Javascript自执行匿名函数(function() { })()的原理浅析
匿名函数就是没有函数名的函数.这篇文章主要介绍了Javascript自执行匿名函数(function() { })()的原理浅析的相关资料,需要的朋友可以参考下 函数是JavaScript中最灵活的一 ...