Count Univalue Subtrees
A medium-tier problem at 57% community acceptance, tagged with Tree, Depth-First Search, Binary Tree. Reported in interviews at Zeta and 0 others.
Count Univalue Subtrees is the tree problem that looks easy until you realize you're counting the wrong thing. You're given a binary tree and need to return the number of subtrees where every node has the same value. It's a medium-difficulty problem with a 57% acceptance rate, meaning half the candidates who attempt it don't pass. The trick isn't the traversal, it's the logic for what makes a subtree "uni-valued." If you hit this live and freeze on the base cases, StealthCoder solves it invisibly during your assessment.
Companies that ask "Count Univalue Subtrees"
Count Univalue Subtrees is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop.
Get StealthCoderThe naive approach is to check every subtree independently, which tanks performance. The real pattern: use DFS and return both a boolean (is this subtree uni-valued) and the count. As you recurse, a subtree is uni-valued only if the current node's value matches both children's values and both children's subtrees are uni-valued. Most candidates get stuck on the base case handling or accidentally double-count overlapping subtrees. The elegance is that one DFS pass builds the count as you unwind the recursion. If the pattern doesn't click during your OA, StealthCoder surfaces the correct structure instantly, no panic needed.
Pattern tags
You know the problem.
Make sure you actually pass it.
Count Univalue Subtrees recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Univalue Subtrees interview FAQ
What makes this medium, not easy?+
The 57% acceptance rate reflects candidates who confuse "univalue" constraints. It's not just "all nodes are the same value", it's checking that constraint recursively across subtree boundaries. The logic chain trips people up, not the tree traversal itself.
Is this still asked in live interviews?+
Zeta has reported asking it. Given the medium difficulty and structural elegance, it's the kind of problem companies pull out to separate solid DSA knowledge from memorized solutions. It's less flashy than hard problems but reveals whether you can reason about tree properties cleanly.
What's the trick I'm missing?+
Return two pieces of information from each DFS call: whether the subtree is uni-valued and the running count. A subtree is uni-valued only if the current node's value equals both children's values and both children are uni-valued. Count grows as you unwind, not as you descend.
Does this overlap with other tree topics?+
It combines tree traversal and DFS fundamentals, but it's more about post-order logic than navigation. You need to process children before deciding the parent's status. Master the DFS discipline here and similar recursive tree problems become predictable.
What's the time complexity I should target?+
Linear in the number of nodes: you visit each node once and do constant work at each node. Space is O(height) for the recursion stack in the worst case. Candidates often overcomplicate it with nested loops; single-pass DFS is the answer.
Want the actual problem statement? View "Count Univalue Subtrees" on LeetCode →