FastPrepCount One-Valued Regions in a Binary Tree
Problems/Count One-Valued Regions in a Binary Tree

Count One-Valued Regions in a Binary Tree

Google logoGoogle● EasyINTERNNEW GRADPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives the definition of a region in a binary tree, the goal of counting them, and the DFS idea. It does not say what to return for an empty tree, the input limits, or a function signature. Our version is about 96% the same as the reported question.

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
root
1110011

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
root
011

The two value-1 children are separated by their value-0 parent, so they belong to different regions.

Example 3

root = []return = 0
root
empty

An empty tree contains no value-1 regions.

Constraints

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

More Google Phone Screen questions

  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
  • Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Logger Rate Limiter with Every-Arrival HistoryPHONE SCREEN · Seen Nov 2026

Similar questions at other companies

  • Merge Two Binary TreesSnowflake · PHONE SCREENSnowflake interview questions
  • Sum All Values in a Binary TreeDuolingo · PHONE SCREENDuolingo interview questions
  • Sum Root-to-Leaf Binary NumbersOracle · PHONE SCREENOracle interview questions
  • Symmetric Binary TreeDocuSign · PHONE SCREENDocuSign interview questions
  • Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
  • Maximum Value in a Binary TreeAkuna Capital · PHONE SCREENAkuna Capital interview questions
  • Path SumSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Binary Tree Preorder TraversalAnduril · ONSITE INTERVIEWAnduril interview questions
  • Root-to-Leaf Paths as StringsAdobe · PHONE SCREENAdobe interview questions
  • Root-to-Node Path in a Binary TreeAmazon · ONSITE INTERVIEW

Interview reports that mention this question

  • Google Intern Grid and Tree Rounds: 7 Experiences (Part 2 of 8)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int countOneRegions(TreeNode root) {
      // Write your code here.
    }
}
Checking account…