Skip to content

250 Count Univalue Subtrees – Medium

Problem:

Given a binary tree, count the number of uni-value subtrees.

A Uni-value subtree means all nodes of the subtree have the same value.

For example: Given binary tree,

          5
         / \
        1   5
       / \   \
      5   5   5

return 4.

Thoughts:

This problem can be solved by using recursion easily. We will need a function to return if a tree is having all the same values. Say the function is isUni(), then the condition for a tree to be a univalue tree is that node.left and node.right are both univalue tree and they have the same val with the node.

Also we will need a counter to keep the numbers of univalue subtree. In the solution below, I am using a class property. If not, we can use a TreeNode variable and pass it into isUni() so that each time inside of isUni function, it’s modifying the same variable.

Solutions:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private int count = 0;
    public int countUnivalSubtrees(TreeNode root) {
        if (root == null) {
            return 0;
        }
        count = 0;
        isUni(root);
        return count;
    }
    private boolean isUni(TreeNode node) {
        if (node == null) {
            return true;
        }
        boolean left = isUni(node.left);
        boolean right = isUni(node.right);
        if (left == true && right == true) {
            if (node.left != null && node.left.val != node.val) {
                return false;
            }
            if (node.right != null && node.right.val != node.val) {
                return false;
            }
            count ++;
            return true;
        }
        return false;

    }
}