Count Univalue Subtrees
2016-06-23 14:06
393 查看
参考:点击打开链接
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int count = 0;
public int countUnivalSubtrees(TreeNode root) {
helper(root);
return count;
}
private boolean helper(TreeNode root) {
if (root == null) {
return true;
}
boolean left = helper(root.left);
boolean right = helper(root.right);
///if (left && right && (left == null || left.val == root.val) && (right == null || right.val == root.val)) {
if (left && right && (root.left == null || root.left.val == root.val) && (root.right == null || root.right.val == root.val)) {
count++;
return true;
}
return false;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int count = 0;
public int countUnivalSubtrees(TreeNode root) {
helper(root);
return count;
}
private boolean helper(TreeNode root) {
if (root == null) {
return true;
}
boolean left = helper(root.left);
boolean right = helper(root.right);
///if (left && right && (left == null || left.val == root.val) && (right == null || right.val == root.val)) {
if (left && right && (root.left == null || root.left.val == root.val) && (root.right == null || root.right.val == root.val)) {
count++;
return true;
}
return false;
}
}
相关文章推荐
- 判断request是否是ajax
- (转)详解 Secure Boot 和 Winows 8 及 UEFI启动的关系
- Errors occurred during the build.
- easyui pivotgrid统计,数据加载
- Java学习笔记(16)JavaFX UI Controls and Multimedia JavaFX UI控件和多媒体
- Top K Frequent Elements
- Android初级之路--------UI控件
- UIView的xib关联问题
- ios开源框架——UITableView+FDTemplateLayoutCell优化UITableViewCell高度计算
- sql insert values 后面的值是单引号,还是没有引号问题由其表中数据格式而定
- UI控件之ScrollView(滚动条)
- sqlmap批量扫描burpsuite请求日志记录
- SQL Server 中master..spt_values的应用
- iOS 蓝牙开发 UUID不能使用 isConnected不能使用的问题解决
- Masonry和FDTemplateLayoutCell搭配使用「UITableview自适应内容高度」
- Arduino SIM900 AT+CGATT=1 出错
- [leetcode] 347. Top K Frequent Elements
- Result Maps collection does not contain value for frontpreviewprofitManage.cdata
- MFC线程相关问题
- android-Building Accessibility Services