Talentd
FleetCode

250. Count Univalue Subtrees

Medium57.5% acceptance4 companiesPremium elsewhere, free here

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

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

Example 1:

Input: root = [5,1,5,5,5,null,5]
Output: 4

Example 2:

Input: root = []
Output: 0

Example 3:

Input: root = [5,5,5,5,5,null,5]
Output: 6

Constraints:

  • The number of the node in the tree will be in the range [0, 1000].
  • -1000 <= Node.val <= 1000

Asked at 4 companies

10:13
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[5,1,5,5,5,null,5]
Input
[]
Input
[5,5,5,5,5,null,5]
Your code is saved on this device.Solution