Reported September 2026
Googletree

Count One-Valued Regions in a Binary Tree

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

Google, September 2026. The input is a binary tree with up to 100000 nodes, and that size kills anything that restarts a search from every node. You need one pass. The task is counting connected regions of 1-valued nodes, and the pattern is tree traversal. It looks like a flood-fill problem, but a tree makes it much simpler than that. Candidates who overthink it build visited sets and graph structures they don't need. If you blank on the single-pass idea during the live OA, StealthCoder runs invisibly as a safety net and gives you the solution while you keep typing.

The problem

Given the root of a binary tree whose node values are only 0 or 1, return the number of connected regions formed by nodes with value 1.
Two value-1 nodes belong to the same region when one can reach the other by following parent-child edges through only value-1 nodes.
An empty tree contains zero regions.

Function
countOneRegions(root: TreeNode) → int

Examples
Example 1
root = [1,1,0,1,0,1,1]
return = 3
The root and the value-1 nodes in its left subtree form one region. The two value-1 children beneath the value-0 right child each start a separate region.
Example 2
root = [0,1,1]
return = 2
The two value-1 children are separated by their value-0 parent, so they belong to different regions.
Example 3
root = []
return = 0
An empty tree contains no value-1 regions.

Constraints
The tree contains at most 100000 nodes.
Every node value is either 0 or 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: a region starts exactly where a 1-node has no 1-valued parent. So count the nodes with value 1 whose parent is either missing (the root) or has value 0. Do a DFS or BFS and pass the parent's value down. When you see a 1 and the parent value is not 1, increment the counter. No visited set, no union-find, no second pass. That's O(n) time. The pitfall is recursion depth. With 100000 nodes, a skewed tree can overflow the call stack in some languages, so use an iterative stack or queue if you're unsure. Another miss is counting 1-nodes with a 0 child as region boundaries, which is backwards. Check Example 2: root 0, two 1 children, answer 2. Both children start regions because the parent is 0. StealthCoder is the hedge on the live OA if the parent-check idea slips away under pressure.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Count One-Valued Regions in a Binary Tree 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count One-Valued Regions in a Binary Tree FAQ

What's the trick to Count One-Valued Regions in a Binary Tree?+

Count the 1-nodes whose parent isn't a 1. Every region has exactly one topmost node, and that node is either the root or sits under a 0. Traverse once, carry the parent's value, and increment when you hit a 1 with a non-1 parent.

How hard is this Google OA question really?+

Easy to medium. The traversal is basic, but people overcomplicate it with flood fill or union-find. Once you see that each region has a unique top node in a tree, it's about ten lines of code.

Will recursion break with 100000 nodes?+

It can. A fully skewed tree means 100000 stack frames, which overflows in some languages. Use an iterative DFS with an explicit stack, or BFS with a queue, storing each node together with its parent's value. That removes the risk.

What's the time and space complexity?+

Time is O(n) since each node is visited once. Space is O(h) for recursive DFS, where h is the tree height, or O(n) worst case for an iterative stack or queue. No extra visited structure is needed because trees have no cycles.

How do I prepare for this in 48 hours?+

Write tree DFS and BFS from memory, both recursive and iterative. Practice passing state like the parent value down the traversal. Then test your code on the three examples, especially the empty tree returning 0 and the all-zeros tree.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it