Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Count Univalue Subtrees
MEDIUM
30 mins
69 upvotes
Trees
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Trees
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Count Univalue Subtrees

Contributed by
Ashwani
Medium
yellow-spark
0/80
Avg time to solve 30 mins
Success Rate 70 %
Share
69 upvotes

Problem Statement

You are given a binary tree. Return the count of unival sub-trees in the given binary tree. In unival trees, all the nodes, below the root node, have the same value as the data of the root.

For example: for the binary tree, given in the following diagram, the number of unival trees is 5.

alt-text

Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
1 1 1 -1 -1 -1 -1
Sample Output 1:
3
Sample Input 2:
1 2 3 2 -1 3 4 -1 -1 3 3 -1 -1 -1 -1 -1 -1
Sample Output 2:
6
Explanation to Sample Input 2:
The input binary tree will be represented as 

alt-text

In the above diagram, the orange marked nodes are the root nodes of the unival sub-trees for the given binary tree.
Reset Code
Full screen
Auto
copy-code
Console