Check Whether All Binary-Tree Nodes Carry One Value

Quick Overview

Check Whether All Binary-Tree Nodes Carry One Value. Return true if every node of the binary tree rooted at root has the same value, and false otherwise.

Check Whether All Binary-Tree Nodes Carry One Value

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Return true if every node of the binary tree rooted at `root` has the same value, and false otherwise. ```cpp class Solution { public: bool isUnivalTree(TreeNode* root); }; ``` ### Examples **Example 1:** ```text Input: root = [7,7,null,7] Output: true Explanation: All three existing nodes have value7. ``` **Example 2:** ```text Input: root = [4,4,4,null,5] Output: false Explanation: The node with value5 differs from the root value4. ``` ### Constraints - The tree contains1 to100 nodes. - `0 <= Node.val < 100`. ### Contract considerations The original input is a TreeNode pointer; level-order examples describe shape only. ```hint Read the boundary condition One fixed reference value is enough to check every node. ``` ### Boundary reminders The original input is a TreeNode pointer; level-order examples describe shape only.

Overview: Check Whether All Binary-Tree Nodes Carry One Value. Return true if every node of the binary tree rooted at root has the same value, and false otherwise.

Amazon logo
Amazon
Jun 22, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return true if every node of the binary tree rooted at root has the same value, and false otherwise.

class Solution {
public:
    bool isUnivalTree(TreeNode* root);
};

Examples

Example 1:

Input: root = [7,7,null,7]
Output: true
Explanation: All three existing nodes have value7.

Example 2:

Input: root = [4,4,4,null,5]
Output: false
Explanation: The node with value5 differs from the root value4.

Constraints

  • The tree contains1 to100 nodes.
  • 0 <= Node.val < 100 .

Contract considerations

The original input is a TreeNode pointer; level-order examples describe shape only.

Boundary reminders

The original input is a TreeNode pointer; level-order examples describe shape only.