CT020

Unival Sacred Groves

MediumAcceptance: 0.0%

A sacred grove in Codetikki is a binary tree where every node has a value. A subtree is called "unival" if all its nodes share the same value. Count the number of unival subtrees in a given binary tree. Input: a binary tree given in level-order as space-separated values, with "null" for absent nodes. Output: the count of unival subtrees.

Example 1:

Input: a a a null a null a
Output: 5

Example 2:

Input: a a b
Output: 2

Constraints:

1 <= number of nodes <= 10^4 Node values are single lowercase letters or null

Tags:

trees recursion dfs
Loading...
Test Cases:No test cases
No test cases available.
Coding Problem Not Found | CodeTikki