Count Univalue Subtrees - Problem
Count Univalue Subtrees is a fascinating tree traversal problem that tests your understanding of recursive depth-first search and tree structure analysis.

Given the root of a binary tree, you need to count how many subtrees are "uni-value" - meaning all nodes in that subtree have the exact same value.

Key Points:
  • A single node is always a uni-value subtree
  • An empty tree (null node) is not counted
  • A subtree is uni-value only if ALL its nodes have the same value
  • You need to check every possible subtree in the tree

Goal: Return the total count of uni-value subtrees found in the entire binary tree.

Input & Output

example_1.py โ€” Basic Tree
$ Input: root = [5,1,5,5,5,null,5]
โ€บ Output: 4
๐Ÿ’ก Note: The tree has 4 uni-value subtrees: three leaf nodes with value 5, and one subtree with root at the right child of root (containing two nodes with value 5).
example_2.py โ€” Single Node
$ Input: root = [1]
โ€บ Output: 1
๐Ÿ’ก Note: A single node is always a uni-value subtree by definition, so the answer is 1.
example_3.py โ€” All Same Values
$ Input: root = [5,5,5,5,5,5,5]
โ€บ Output: 7
๐Ÿ’ก Note: When all nodes have the same value, every possible subtree is uni-value. With 7 nodes, there are 7 different subtrees (each node can be a root of a subtree).

Visualization

Tap to expand
๐ŸŒณ Uni-Value Subtree Detection: Bottom-Up Approach515115โœ“ 1โœ“ 1โœ“ 1โœ“ Subtree: 3โœ“ Subtree: 2โœ— Not Uniform๐ŸŽฏ Key Insight: Bottom-Up ProcessingProcess children first, then determine if parent forms uni-value subtreeTotal uni-value subtrees found: 5 (3 leaves + 2 internal subtrees)
Understanding the Visualization
1
๐Ÿ  Start with Individual Houses
Every single house (leaf node) is automatically a uniform neighborhood
2
๐Ÿ˜๏ธ Check Small Streets
Look at parent-child relationships - do they form uniform neighborhoods?
3
๐Ÿ™๏ธ Build Up to Districts
Continue building upward, checking larger and larger neighborhoods
4
๐Ÿ“Š Count All Discoveries
Sum up all uniform neighborhoods found at every level
Key Takeaway
๐ŸŽฏ Key Insight: The optimal approach processes the tree bottom-up, checking each subtree exactly once. This eliminates redundant work and achieves O(n) time complexity by combining the validation and counting in a single traversal.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each node is visited exactly once during the DFS traversal

n
2n
โœ“ Linear Growth
Space Complexity
O(h)

Recursion stack depth equals tree height h, O(log n) for balanced trees

n
2n
โœ“ Linear Space

Constraints

  • The number of nodes in the tree is in the range [0, 1000]
  • -1000 โ‰ค Node.val โ‰ค 1000
  • Tree nodes can have negative values
  • An empty tree returns 0
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
76.3K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen