dfs
that takes a node as an argument and returns a tuple (is_uni, count)
, where is_uni
is a boolean indicating if the subtree rooted at the node is a uni-value subtree, and count
is the number of uni-value subtrees within that subtree.null
, return (True, 0)
since a null node is trivially a uni-value subtree and contributes 0 to the count.dfs
on the left and right children of the node.(True, count)
.(False, count)
without incrementing.dfs(root)
and taking the second element of the returned tuple.