[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 ...
随机推荐
- Android手机出现"已安装了存在签名冲突的同名数据包"的原因及解决办法
http://blog.csdn.net/dyllove98/article/details/8830264 如果你不是开发者:如果你在android上更新一个已经安装过较早版本软件时,安装到最后一步 ...
- android studio环境搭建-笔记1
自己干了几年测试(功能性的),最近比较闲,就自己学习下android(以前也有所接触,但那是几年前的一点皮毛,都忘记了). 先搭建谷歌推出的android studio(以前用eclipse搭建总觉得 ...
- android中使用Intent在activity之间传递数据
android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...
- OC与Swift的区别一(文件结构)
1.文件后缀名 oc的文件后缀名为:头文件.h 主体文件.m swift文件后缀名为:.swift 2. 代码分隔符 oc中使用分号;作为代码分隔符 swift中无需使用代码分隔符,以行作为代码分隔 ...
- Java中printStackTrace()、toString()、getMessage()的区别
一.三者之间的关系图: 二.演示 1.printStackTrace()演示: public class Test { public int div(int a, int b) { ...
- 无刷新分页 Ajax,JQuery,Json
1.数据库设计 字段:Id(int) , Msg(varchar(MAX)) , PostDate(datetime) 2.自定义SQL查询方法(强类型DataSet) //SelectCount() ...
- .NET多线程编程(转)
在.NET多线程编程这个系列我们讲一起来探讨多线程编程的各个方面.首先我将在本篇文章的开始向大家介绍多线程的有关概念以及多线程编程的基础知识;在接下来的文章中,我将逐一讲述.NET平台上多线程编程的知 ...
- Csharp Winfrom 多串口通信
Csharp 多串口通信 顾名思义,多串口通信,普通的PC机一般只有一个串口,现在很多家用的PC都没有串口,那么问题来了,如何保证多串口呢? 有一种神器,MOXA CP-168U Series PCI ...
- 传感器 -UIAccelerometer
// ios 4 之前 UIAccelerometer // ios 5 <CoreMotion/CoreMotion.h> #import "ViewController.h& ...
- 几MB的大图片变成几百KB
使用windows自带的“画图”工具就可以. 1.用“画图”打开图片. 2.点击“重新调整大小” 弹出如下窗口 修改这里的“水平”和“垂直”,如都从100改为30.改完之后,点击确定,最后再“保存”或 ...