[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#类和成员定义
1 定义类 C#用关键字class来定义类.默认情况下,类声明为内部(internal)的,即只有当前项目中的代码才能访问它.与之相对应的,还可以用public关键字来修饰,这样该类还可以由其 ...
- Android ImageSwitcher和Gallery的使用
前几天,听说室友的老师要求他们做一个图片效果.其效果如下图所示(可左右滑动切换图片): 我当时晃眼一看,第一感觉好高级的样子.我还没做过这种效果呢,但室友说他们同学已经有人做出来了,我觉得既然有人做出 ...
- LiangNa Resum
LiangNa AnShan Street, YangPu, NY @.com OBJECTIVE: Seeking a position to contribute my skills and ed ...
- C#面向对象的一些东西
最近在复习C#面向对象,也就是说常说的3大特性:封装.继承和多态.首先说一下封装,其实封装最大的目的也是为了实现代码的解耦和重用.代码也是安全的(对外它隐藏了具体的实现,就好比我们拿个遥控器就能操作电 ...
- redhat5.8无法进入图形界面
解决办法: 删除/etc/X11/xorg.conf文件
- OC 消息传递机制
消息传递(message passing)的概念是Smalltalk语言的核心原则之一,有时Smalltalk和Objective-C被称为面向消息的语言,通常“消息”一词的含义和“方法”是相同的. ...
- ABP手机端调用API时的CORS
这个问题其实很早就考虑了,当时因为也没有特别着急去解决这个问题,就一直拖着.... 好吧,拖延症是不好的,所有不懒得做的,终将会逼着你去再很短的时间内去解决问题...实现项目 改写一个已有的webfo ...
- MongoDB笔记(五)深入学习
系列一:http://www.cnblogs.com/huangxincheng/category/355399.html系列二:http://www.cnblogs.com/lipan/catego ...
- 【python之路7】python基本数据类型(一)
一.运算符 1.算数运算符 +.-.*./.%(求余数).//(取整数部分) python2.x中,如果计算浮点数其中一个数字必须是浮点数否则按整数计算: 如python2.7中:print 9/2 ...
- Java 之 网络编程
1.OSI模型 a.全称:开放系统互联 b.七层模型:应用层.表示层.会话层.传输层.网络层.数据链路层.物理层 c.注意:OSI模型 是所谓的 理论标准 2.TCP/IP模型 a.四层模型:应用层. ...