
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Checking for univalued Binary Search Tree in JavaScript
Univalued Binary Search Tree
A binary search tree is univalued if every node in the tree has the same value.
Problem
We are required to write a JavaScript function that takes in the root of a BST and returns true if and only if the given 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;
Example
The code for this will be −
class Node{ constructor(data) { this.data = data; this.left = null; this.right = null; }; }; class BinarySearchTree{ constructor(){ // root of a binary seach tree 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); }; }; }; }; const BST = new BinarySearchTree(); BST.insert(5); BST.insert(5); BST.insert(5); BST.insert(3); BST.insert(5); BST.insert(6); const isUnivalued = (root) => { const helper = (node, prev) => { if (!node) { return true } if (node.data !== prev) { return false } let isLeftValid = true let isRightValid = true if (node.left) { isLeftValid = helper(node.left, prev) } if (isLeftValid && node.right) { isRightValid = helper(node.right, prev) } return isLeftValid && isRightValid } if (!root) { return true } return helper(root, root.data) }; console.log(isUnivalued(BST.root));
Output
And the output in the console will be −
false
- Related Articles
- Binary Search Tree in Javascript
- Searching for values in an Javascript Binary Search Tree
- Binary Search Tree Class in Javascript
- Implementing a Binary Search Tree in JavaScript
- Searching for minimum and maximum values in an Javascript Binary Search Tree
- Finding Mode in a Binary Search Tree in JavaScript
- Binary Tree to Binary Search Tree Conversion in C++
- Difference between Binary Tree and Binary Search Tree
- C++ Program to Search for an Element in a Binary Search Tree
- Optimal Binary Search Tree
- Binary Search Tree Iterator in C++
- Validate Binary Search Tree in Python
- Recover Binary Search Tree in C++
- Finding minimum absolute difference within a Binary Search Tree in JavaScript
- Binary Search Tree - Search and Insertion Operations in C++

Advertisements