Validate Binary Search Tree
To check if a binary tree is a valid Binary Search Tree (BST), you can perform an in-order traversal and check that the nodes are visited in strictly ascending order. This will ensure that the left subtree has values less than the root’s value, and the right subtree has values greater than the root’s value, as well as ensuring the subtrees themselves are BSTs.
Here’s a code snippet to determine if a given binary tree is a valid BST:
|
|
This code uses a recursive in-order traversal, where prev
is a one-element list that keeps track of the previously visited node. If at any point a node is encountered that does not have a value greater than the previous node’s value, the function returns False
. If the traversal completes without finding any violations of the BST property, the function returns True
.
The time complexity of this function is (O(n)), where (n) is the number of nodes in the tree, and the space complexity is (O(h)), where (h) is the height of the tree.
title: Valid Binary Search Tree tags: data-flow-direction top-down-tree-processing
The key to solving this problem is to pass the range for each recursive call to make sure that binary search tree property holds for all nodes, not just the parent and child nodes, BST must be valid for nodes that are even further apart. The data flows from the parent to children.
|
|
- Recursive: left subtree, right subtree. Recursive cases.
- Base cases: 1
- What is the unit of work ? Check to see root.left.val node < root > root.right.val node
- How do we handle both left and right subtrees are also binary search trees
- We have to have a range to check if the nodes satisfy the invariant
|
|
Identifying Problem Isomorphism
“Validate Binary Search Tree” asks you to check if a given tree is a valid Binary Search Tree (BST), which requires checking if the left child of a node is less than the node and the right child is greater than the node, for all nodes.
An isomorphic problem to this is “Recover Binary Search Tree” (LeetCode #99). In this problem, you are given a binary search tree where two nodes have been swapped by mistake, and you have to recover the tree without changing its structure.
To solve both problems, you can use an inorder traversal. In “Validate Binary Search Tree”, you check if each node is greater than the previous one in the inorder traversal. In “Recover Binary Search Tree”, you also do an inorder traversal and look for nodes that are smaller than the previous node, indicating that they are out of place. Then you swap the nodes to fix the BST.
These problems have isomorphism in the sense that they both involve understanding properties of BSTs and using an inorder traversal. They are not exactly identical problems as one involves validating a BST while the other involves fixing a BST.
|
|
Problem Classification
Language Agnostic Coding Drills
Understanding Trees: Understanding the basic concept of tree data structure and its terminology like root, child, sibling, leaf, etc.
Creating a Class: Create a class with its attributes and methods.
Recursive Functions: Understand the concept of recursion and how to use recursive functions to solve problems.
Binary Search Trees: Learn about Binary Search Trees (BST) and their properties. Write a function to check whether a given binary tree is a BST or not.
Inorder Tree Traversal: Understand the inorder tree traversal algorithm and write a function to perform inorder traversal of a binary tree.
Optional and Nonlocal Keywords: Understand the use of optional and nonlocal keywords in Python.
Optional
keyword is used to specify that the parameter of a function or method is optional.Nonlocal
keyword is used to specify that a variable is not local to the function or method in which it is declared.
Targeted Drills in Python
Understanding Trees: Create a simple tree data structure and practice accessing its nodes and values.
Creating a Class:
|
|
- Recursive Functions:
|
|
Binary Search Trees: Write a function to check whether a given binary tree is a BST or not.
Inorder Tree Traversal:
|
|
- Optional and Nonlocal Keywords:
|
|