Is dit jouw bedrijf?
Given a binary tree test if it is a binary search tree
Anoniem
A Binary Search Tree (BST) is a binary tree responding to the following rules: - The LEFT subtree of a given node only contains nodes with keys LESS than the key of that given node - The RIGHT subtree of a given node only contains nodes with keys GREATER than the key of that given node - For any given node, its left and right subtrees must also be BSTs Following those rules, we can notice that an In-Order traversal of a BST returns the keys in ascending order. Thus, to test if a binary tree is a search tree, you just have to do an In-Order traversal of a binary tree, keep track of the previously visited node and check If the value of the currently visited node is less than the previous value. If true, STOP there, you can conclude that it is not a BST. If you reach the end of the tree (visited all nodes) then you can conclude that it is a BST.