From the course: Data Structures in JavaScript: Trees and Graphs

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Solution: Validate a binary tree as BST

Solution: Validate a binary tree as BST

(bright music) - [Instructor] In this video we're going to walk through how to determine if a binary tree is a valid BST. It's a classic problem that tests both your understanding of tree structure and your ability to implement recursive logic cleanly and efficiently. You are given a root of a binary tree and your task is to figure out is this tree a valid BST? Let's quickly recall what defines a BST. The left subtree of a node contains only values less than the nodes values. The right subtree must contain only values greater than the nodes values, and both subtrees themselves must also be valid BSTs. This means we can't just check the immediate children. We have to make sure all values down each path stay within the correct bounds. Let's take a look at example one, a valid BST. Everything checks out. The left children are all less than the root. The right children are all greater than the root, and all subtrees follow the same rule, so the result is true. In example two, it looks…

Contents