Univalued Binary Tree

EasyTreesdfs

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given the root of a non-empty binary tree. Decide whether every node in the tree stores the same value. Return true if all node values are identical and false as soon as any two nodes differ. A single traversal that compares each node against the root's value runs in O(n) time.

Examples

Input: root = [1,1,1,1,1,null,1]
Output: true
All six nodes hold 1.
Input: root = [2,2,2,5,2]
Output: false
The left child of the left child holds 5, which differs from 2.

Constraints

  • 1 <= number of nodes <= 100
  • 0 <= Node.val < 100

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More dfs problems