Count One-Valued Regions in a Binary Tree
Updated
Problem statement
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) → intExamples
Example 1
root = [1,1,0,1,0,1,1]return = 3The 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 = 2The two value-1 children are separated by their value-0 parent, so they belong to different regions.
Example 3
root = []return = 0An empty tree contains no value-1 regions.
Constraints
- The tree contains at most
100000nodes. - Every node value is either
0or1.
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