Validate Binary Search Tree
Understand how to determine whether a binary tree is a valid binary search tree by exploring the rules for node key placement. Learn to use tree depth-first search techniques to verify BST properties efficiently, and practice implementing your solution in a coding environment.
We'll cover the following...
Statement
Given the root of a binary tree, check whether it is a valid binary search tree (BST).
A binary tree is a valid BST if for every node:
-
The left subtree of a node contains only nodes with keys less than the node’s key.
-
The right subtree of a node contains only nodes with keys greater than the node’s key.
-
Both the left and right subtrees are valid BSTs.
Constraints:
-
Node.data -
The tree contains nodes in the range .
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Valid Binary Search Tree
Is the following binary tree a valid binary search tree?
9
/ \
2 10
/ \
7 20
True
False
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in the following coding playground.
// Definition of a binary tree node// class TreeNode {// constructor(data) {// this.data = data;// this.left = null;// this.right = null;// }// }import {TreeNode} from './ds_v1/BinaryTree.js';function validateBst(root){// Replace this placeholder return statement with your codereturn false;}export {validateBst};