本文主要是介绍Count Univalue Subtrees,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
参考:点击打开链接
/*** 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;}
}
这篇关于Count Univalue Subtrees的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!