A unival tree (which stands for “universal value”) is a tree where all nodes under it have the same value.
Given the root to a binary tree, count the number of unival subtrees.
Example
1 2 3 4 5 6 7 8 9
0 / \ 1 0 / \ 1 0 / \ 1 1 return 5;
Solution
We need a helper function to tell us if it is unival tree for current root, and when left and right tell us it’s unival tree, we have to check the value of two subtrees.