# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def countUnivalSubtrees(self, root: TreeNode) -> int:
self.ans = 0
def dfs(node):
if not node:
return None
if not node.left and not node.right:
self.ans += 1
return node.val
left = dfs(node.left)
right = dfs(node.right)
if left == right == node.val or (left is None and right == node.val) or (right is None and left == node.val):
self.ans += 1
return node.val
return inf
dfs(root)
return self.ans
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int count = 0;
boolean is_valid_part(TreeNode node, int val) {
// considered a valid subtree
if (node == null) return true;
// check if node.left and node.right are univalue subtrees of value node.val
// note that || short circuits but | does not - both sides of the or get evaluated with | so we explore all possible routes
if (!is_valid_part(node.left, node.val) | !is_valid_part(node.right, node.val)) return false;
// if it passed the last step then this a valid subtree - increment
count++;
// at this point we know that this node is a univalue subtree of value node.val
// pass a boolean indicating if this is a valid subtree for the parent node
return node.val == val;
}
public int countUnivalSubtrees(TreeNode root) {
is_valid_part(root, 0);
return count;
}
}
class Solution2 {
int count = 0;
boolean is_uni(TreeNode node) {
//base case - if the node has no children this is a univalue subtree
if (node.left == null && node.right == null) {
// found a univalue subtree - increment
count++;
return true;
}
boolean is_unival = true;
// check if all of the node's children are univalue subtrees and if they have the same value
// also recursively call is_uni for children
if (node.left != null) {
is_unival = is_uni(node.left) && is_unival && node.left.val == node.val;
}
if (node.right != null) {
is_unival = is_uni(node.right) && is_unival && node.right.val == node.val;
}
// return if a univalue tree exists here and increment if it does
if (!is_unival) return false;
count++;
return true;
}
public int countUnivalSubtrees(TreeNode root) {
if (root == null) return 0;
is_uni(root);
return count;
}
}
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def countUnivalSubtrees(self, root: Optional[TreeNode]) -> int:
self.count = 0
self.is_valid_part(root, 0)
return self.count
def is_valid_part(self, node, val):
# considered a valid subtree
if node is None: return True
# check if node.left and node.right are univalue subtrees of value node.val
if not all([self.is_valid_part(node.left, node.val),
self.is_valid_part(node.right, node.val)]):
return False
# if it passed the last step then this a valid subtree - increment
self.count += 1
# at this point we know that this node is a univalue subtree of value node.val
# pass a boolean indicating if this is a valid subtree for the parent node
return node.val == val
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def countUnivalSubtrees(self, root: Optional[TreeNode]) -> int:
if root is None: return 0
self.count = 0
self.is_uni(root)
return self.count
def is_uni(self, node: Optional[TreeNode]) -> bool:
# base case - if the node has no children this is a univalue subtree
if node.left is None and node.right is None:
# found a univalue subtree - increment
self.count += 1
return True
is_uni = True
# check if all of the node's children are univalue subtrees and if they have the same value
# also recursively call is_uni for children
if node.left is not None:
is_uni = self.is_uni(node.left) and is_uni and node.left.val == node.val
if node.right is not None:
is_uni = self.is_uni(node.right) and is_uni and node.right.val == node.val
# increment self.res and return whether a univalue tree exists here
self.count += is_uni
return is_uni