Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Count univalue subtrees and Search in rotated sorted array



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Count Same-Value Subtrees

Count Same-Value Subtrees

Problem Statement

Given a binary tree, determine how many subtrees have all nodes containing the same value.

A subtree is considered uniform if every node in that subtree holds an identical value.

Examples

Example 1:
Input: root = [4,3,4,4,4,null,4]

          4
         / \
        3   4
       / \    \
      4   4    4

Output: 4

Here are the 4 uni-value subtrees from the input tree. 
Each of these subtrees has all nodes with the same value:

A single node with value 4 (rightmost leaf).
A single node with value 4 (left child of 3).
A single node with value 4 (right child of 3).
A subtree rooted at the right child of the root (4) with its own right child 4.

Example 2:
Input: root = [2,2,2,2,null,null,2]

          2
         / \
        2   2
       /     \
      2       2

Output: 5

Constraints

  • The total number of nodes in the tree is between 1 and 5000.
  • Node values are integers ranging from 0 to 100.