FastPrepUnique One-Region Sizes in a Binary Tree

Unique One-Region Sizes in a Binary Tree

Google logoGoogle● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

You are given a binary tree whose node values are only 0 or 1. A one-region is a maximal connected component of value-1 nodes using parent-child edges.

Return the distinct region sizes in increasing order. Return an empty array for an empty tree or a tree with no value-1 nodes.

Function

uniqueOneRegionSizes(root: TreeNode) → int[]

Examples

Example 1

root = [1,1,0,1,0,1,1]return = [1,3]

The left connected region has size 3; the two right children form one size-2 region? In level order they share a zero parent, so they are separate size-1 regions, producing sizes 1 and 3.

Example 2

root = [0,0,0]return = []

No node has value 1.

Example 3

root = []return = []

An empty tree has no regions.

Constraints

  • The tree has at most 2000 nodes.
  • Every node value is 0 or 1.

More Google problems

See Google hiring insights
public int[] uniqueOneRegionSizes(TreeNode root) {
    // Write your code here.
}
root[1,1,0,1,0,1,1]
expected[1,3]
Checking account…