Univalued Binary Tree

A binary tree is uni-valued if every node in the tree has the same value.

Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.

Example 1
        1
       / \
      1   1
     / \   \
    1   1   1
Inputroot = [1,1,1,1,1,null,1]
Outputtrue
Every node in the tree has the value 1, so the tree is uni-valued.
Example 2
        2
       / \
      2   2
     / \
    5   2
Inputroot = [2,2,2,5,2]
Outputfalse
The tree contains a node with value 5 while the other nodes have value 2, so it is not uni-valued.

Constraints

  • The number of nodes in the tree is in the range [1, 100].
  • 0 <= Node.val < 100

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate