java 250.计算Univalue Subtrees.java
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 250.计算Univalue Subtrees.java相关的知识,希望对你有一定的参考价值。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int countUnivalSubtrees(TreeNode root) {
int[] count = new int[] {0};
isUnivalSubtrees(root,count);
return count[0];
}
private boolean isUnivalSubtrees(TreeNode root, int[] count) {
if(root == null) return true;
boolean left = isUnivalSubtrees(root.left, count);
boolean right = isUnivalSubtrees(root.right, count);
if(left && right) {
if(root.left != null && root.left.val != root.val) {
return false;
}
if(root.right != null && root.right.val != root.val) {
return false;
}
count[0]++;
return true;
}
return false;
}
}
/*
[]
[5,1,5,5,5,null,5]
[5,1,3,1,1,1]
*/
以上是关于java 250.计算Univalue Subtrees.java的主要内容,如果未能解决你的问题,请参考以下文章
java 250.计算Univalue Subtrees.java
java 250.计算Univalue Subtrees.java
java 250.计算Univalue Subtrees.java
java 250.计算Univalue Subtrees.java
[LeetCode] 250. Count Univalue Subtrees 计算唯一值子树的个数
250. Count Univalue Subtrees