[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 ...
随机推荐
- 20160324 javaweb 之request
package com.dzq.servlet; import java.io.IOException; import javax.servlet.ServletException; import j ...
- 2014年的Google I/O app设计中的材料设计-渣渣的翻译
又是一篇翻译,用了三个多小时.http://android-developers.blogspot.co.id/2014/08/material-design-in-2014-google-io-ap ...
- php中怎么实现后台执行?
http://www.cnblogs.com/zdz8207/p/3765567.html php中实现后台执行的方法: ignore_user_abort(true); // 后台运行set_tim ...
- 常见错误总结_1_对java类进行修改后,无法按修改的类型加载
1.这是因为没有run的原因,对类进行修改一定要run一遍 2.至于要不要重新tomcat部署,取决于你是修改了变量还是方法,拿不定的时候都重新加载一遍看看.
- LINQ2EF-LINQ2SQL-LINQ笔记
例1:In SQL: AND dep_all_code LIKE '" + depAll + "' AND dep_code in (SELECT DISTINCT DEP3 FR ...
- js判断手机浏览器操作系统和微信浏览器的方法
做手机端的前端开发,少不了对手机平台的判断.如,对于app下载,就要判断在Android平台下就显示Android下载提示:在iOS平台下就显示iOS下载提示. 今天就为大家介绍一下用js判断手机客户 ...
- 版本控制:集中式 vs 分布式
集中式 CVCS的版本库集中存放在中央服务器,而工作时都是用自己的电脑,所以要先从中央服务器取得最新的版本,然后工作完后再将自己的代码推送给中央服务器. CVS:最早的.开源.免费.由于自身设计的问题 ...
- java之内存管理
对于JVM的垃圾回收机制来说,是否回收一个对象的标准在于:是否还有引用变量引用该对象,只要有引用变量引用该对象,垃圾回收机制就不会回收它. 强引用:创建一个对象,并把这个对象赋给一个引用变量.这种引用 ...
- [USACO1.1.4]坏掉的项链Broken Necklace
P1203 [USACO1.1]坏掉的项链Broken Necklace 标签 搜索/枚举 USACO 难度 普及- 题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N&l ...
- IOS 学习笔记 2015-04-15 Xcode 工程模板分类
一 Application类型 我们大部分呢的开发工作都是使用Application类型的模板创建IOS程序开始的,该类型包括5个模板1 Master-Detail-Application ...