Univalued Binary Tree

A binary tree is uni-valued when every node in it holds the same value. Given the root of a binary tree, return true if the tree is uni-valued and false otherwise.

30:00

Univalued Binary Tree
easy
Free written solution
Topics
Companies

A binary tree is uni-valued when every node in it holds the same value. Given the root of a binary tree, return true if the tree is uni-valued and false otherwise.

Example 1:
Input: {"root":[1,1,1,1,1,null,1]}
Output: true
Constraints:
  • The number of nodes in the tree is in the range [1,100][1, 100].

  • 0≤Node.val<1000 \leq \text{Node.val} < 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"root":[1,1,1,1,1,null,1]}

The root holds 1, so every node must equal 1. Push the root onto the stack.

target (root value)

1

this pop

-

0 of 6 checked

111111

stack

1

pop, compare with target, push children

Variables
VariableValue
target1
node-
stack[1]
checked0
DepthFunction Call
Stack empty
0/7