Question
Write an algorithm to verify if a Binary Tree (BT) is a Binary Search Tree (BST).
Definitions
BT: A binary tree in computer science is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right".
BST: A binary search tree (BST) (aka ordered or sorted binary tree) in computer science is a node-based binary tree data structure having following properties:[1]
- 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 must also be binary search trees.
Solutions
1. An in-order traversal of the binary tree should yield a sorted list of node keys.
2. Write a function (iterative/recursive) to verify the three basic properties of a BST
as described above.
References
1. CSLibrary Stanford
This discusses many interesting functions on binary trees and binary search trees: Lookup(), Insert(), Delete(), NoOfNodes(), MaxDepth(), MinValue(), PathSum(), Mirror(), doubleTree(), sameTree(), countTrees()
2. UCB Video Lecture on Binary Search Trees
3. UCB Video Lecture Series on Data Structures