LeetcodeMay 26, 2025

Univalued Binary Tree

Hazrat Ali

Leetcode

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

 

Example 1:

Input: root = [1,1,1,1,1,null,1]
Output: true

Example 2:

Input: root = [2,2,2,5,2]
Output: false


Solution
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
const isUnivalTree = root => {
  if (!root) {
    return true;
  }

  if (root.left && root.left.val !== root.val) {
    return false;
  }

  if (root.right && root.right.val !== root.val) {
    return false;
  }


Comments