Count Uni-Valued Subtrees
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reportedly served Count Uni-Valued Subtrees in September 2026, and the trap is the one that makes a clean-looking solution quietly return the wrong number. You've got an OA coming and a binary tree staring at you. The task is simple: count subtrees where every node shares one value. It's a tree DFS problem, and the whole thing hinges on one detail about what a child tells its parent. If you blank on the traversal order during the live OA, StealthCoder sits invisibly on your screen as a safety net. Know the trick first and you probably won't need it.
The problem
Given the root of a binary tree, return the number of subtrees whose nodes all have the same value. Every node defines one subtree consisting of that node and all of its descendants. A leaf is therefore always a uni-valued subtree. Function countUnivalSubtrees(root: TreeNode) → int Examples Example 1 root = [5,1,5,5,5,null,5] return = 4 The three leaf fives and the right subtree rooted at five are uni-valued. Example 2 root = [1] return = 1 A leaf is uni-valued. Example 3 root = [] return = 0 An empty tree contains no subtree. Constraints The tree contains between 0 and 500 nodes. -1000 ≤ Node.val ≤ 1000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Do a post-order DFS. Each call returns whether its subtree is uni-valued, and a global counter increments when it is. A node qualifies only if both children are uni-valued and each existing child's value equals the node's value. Null children count as fine. The pitfall is the naive check: comparing only the node to its immediate children without confirming the children's subtrees are themselves uni-valued. Look at Example 1. The root's value of 5 matches the right child, but the left child holds a 1, so the root fails. Another trap is short-circuiting. If you return early after the left side fails, you skip the right side and miss its count. Always recurse into both children before combining. Empty tree returns 0, a leaf returns true and counts as 1. That's O(n) time and O(h) stack, and 500 nodes is no stress. If the recursion logic slips mid-OA, StealthCoder is the hedge that hands you the working version.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Count Uni-Valued Subtrees cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as count univalue subtrees. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Uni-Valued Subtrees FAQ
What's the trick in Count Uni-Valued Subtrees?+
Go bottom-up. Recurse into both children first, then decide if the current node is uni-valued. A node counts only if both children's subtrees are uni-valued and any existing child has the same value as the node. Increment a counter each time that holds.
Why does my solution undercount or overcount?+
Usually you either skipped recursing into the second child after the first failed, or you only compared values with direct children without checking their subtrees. Both calls must always run, and the result is combined afterward with an AND.
How hard is this one really?+
Easy to medium. It's a standard post-order tree problem, and the logic is about ten lines. The difficulty is the edge handling: null children, leaves, and not short-circuiting. If you've written any bottom-up tree DFS, it's quick.
What are the edge cases to test?+
Test an empty tree (returns 0), a single node (returns 1), a tree where every value is identical (every node counts), and the Example 1 shape where the root matches one child but not the other. Those four catch nearly every bug.
How do I prepare for this in 48 hours?+
Write the post-order DFS from scratch twice, once recursive with a counter and once returning a pair of count and flag. Then try two related tree problems that return info upward, like tree height or balance check. Focus on the pattern, not memorizing code.