Checking for univalued Binary Search Tree in JavaScript

A binary search tree is univalued if every node in the tree has the same value. This means all nodes must contain identical data values for the tree to be considered univalued.

Problem

We need to write a JavaScript function that takes the root of a BST and returns true if the tree is univalued, false otherwise.

For example, if the nodes of the tree are:

const input = [5, 5, 5, 3, 5, 6];

Then the output should be:

const output = false;

Binary Search Tree Implementation

First, let's create a BST class to build our tree structure:

class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

class BinarySearchTree {
    constructor() {
        this.root = null;
    }
    
    insert(data) {
        var newNode = new Node(data);
        if (this.root === null) {
            this.root = newNode;
        } else {
            this.insertNode(this.root, newNode);
        }
    }
    
    insertNode(node, newNode) {
        if (newNode.data < node.data) {
            if (node.left === null) {
                node.left = newNode;
            } else {
                this.insertNode(node.left, newNode);
            }
        } else {
            if (node.right === null) {
                node.right = newNode;
            } else {
                this.insertNode(node.right, newNode);
            }
        }
    }
}

Univalued Tree Check Algorithm

The algorithm uses a helper function that recursively checks if all nodes have the same value as the root:

const isUnivalued = (root) => {
    const helper = (node, expectedValue) => {
        if (!node) {
            return true;
        }
        
        if (node.data !== expectedValue) {
            return false;
        }
        
        let isLeftValid = true;
        let isRightValid = true;
        
        if (node.left) {
            isLeftValid = helper(node.left, expectedValue);
        }
        
        if (isLeftValid && node.right) {
            isRightValid = helper(node.right, expectedValue);
        }
        
        return isLeftValid && isRightValid;
    };
    
    if (!root) {
        return true;
    }
    
    return helper(root, root.data);
};

// Create BST with mixed values
const BST = new BinarySearchTree();
BST.insert(5);
BST.insert(5);
BST.insert(5);
BST.insert(3);
BST.insert(5);
BST.insert(6);

console.log("Mixed values tree:", isUnivalued(BST.root));

// Create univalued BST
const univarBST = new BinarySearchTree();
univarBST.insert(7);
univarBST.insert(7);
univarBST.insert(7);
univarBST.insert(7);

console.log("Univalued tree:", isUnivalued(univarBST.root));
Mixed values tree: false
Univalued tree: true

How It Works

The algorithm works by:

  • Starting with the root node's value as the expected value
  • Recursively traversing all nodes in the tree
  • Comparing each node's data with the expected value
  • Returning false immediately if any mismatch is found
  • Using early termination to optimize performance

Edge Cases

// Empty tree
console.log("Empty tree:", isUnivalued(null));

// Single node
const singleNode = new Node(42);
console.log("Single node:", isUnivalued(singleNode));
Empty tree: true
Single node: true

Conclusion

The univalued BST check uses recursive traversal to compare all nodes against the root value. The algorithm efficiently handles edge cases and uses early termination for optimal performance.

Updated on: 2026-03-15T23:19:00+05:30

191 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements