Validating Binary Search Tree

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

A binary search tree (BST) is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child. A valid BST is a tree in which all the nodes in the left subtree of a node have a value less than the node's value, and all the nodes in the right subtree have a value greater than the node's value. In this challenge, we will determine if a given tree is a valid binary search tree.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/graphs_trees("`Graphs Trees`") algorithm/BasicAlgorithmsGroup -.-> algorithm/recursion_dynamic("`Recursion Dynamic`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/PythonStandardLibraryGroup -.-> python/os_system("`Operating System and System`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/graphs_trees -.-> lab-268822{{"`Validating Binary Search Tree`"}} algorithm/recursion_dynamic -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/variables_data_types -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/strings -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/booleans -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/type_conversion -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/conditional_statements -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/tuples -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/function_definition -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/default_arguments -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/importing_modules -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/standard_libraries -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/classes_objects -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/constructor -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/polymorphism -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/encapsulation -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/raising_exceptions -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/os_system -.-> lab-268822{{"`Validating Binary Search Tree`"}} python/build_in_functions -.-> lab-268822{{"`Validating Binary Search Tree`"}} end

Bst Validate

Problem

The problem is to write a Python function that takes the root node of a binary tree as input and determines if it is a valid binary search tree. A binary tree is a valid binary search tree if and only if the following conditions are met:

  1. The left subtree of a node contains only nodes with values less than the node's value.
  2. The right subtree of a node contains only nodes with values greater than the node's value.
  3. Both the left and right subtrees are themselves valid binary search trees.

Requirements

To solve this challenge, the following requirements must be met:

  • The function should be able to handle binary trees with duplicates.
  • If the function is called with a None input, it should raise an exception.
  • The Node class should already be defined.
  • The binary tree should fit in memory.

Example Usage

Valid:
      5
    /   \
   5     8
  /     /
 4     6
        \
         7

Invalid:
      5
    /   \
   5     8
  / \   /
 4   9 7

Summary

In this challenge, we have learned how to determine if a given binary tree is a valid binary search tree. We have also discussed the requirements for this challenge, including the fact that the function should be able to handle duplicates, raise an exception if called with a None input, and assume that the Node class is already defined.