P14265 [GeeksforGeeks] Single valued subtree
普及
题目描述

Given a binary tree, count the number of Single Valued Subtrees. A Single Valued Subtree is one in which all the nodes have same value.
Example 1
Input :
5
/ \
1 5
/ \ \
5 5 5
Output : 4
Explanation :
There are 4 subtrees with single values. Three leaf nodes and the subtree whose root is the right child of the root.
Example 2:
Input:
5
/ \
4 5
/ \ \
4 4 5
Output: 5
Explanation:
There are five subtrees with single values.
Your task :
You don't have to read input or print anything. Your task is to complete the function singlevalued() which takes the root of the tree as input and returns the count of single valued subtrees.

Expected Time Complexity : O(n)
Expected Auxiliary Space : O(n)

Constraints :
1 <= n <= 10^5

输入格式

见题面

输出格式

见题面

样例输入
5 / \ 1 5 / \ \ 5 5 5
样例输出
4