Write a function that tests whether a binary tree is a binary . c++ Note: in a b
ID: 3566754 • Letter: W
Question
Write a function that tests whether a binary tree is a binary . c++
Note: in a binary search tree, for each node, the values of all nodes in the right subtree
are greater than the node, and the values of all nodes in the left subtree are less than the
node. For instance, the following tree is not a binary search tree, because 15 must be less
than 10.
10
7 12
6 15 5 13
Explanation / Answer
int isBST(struct node* node) { return(isBSTUtil(node, INT_MIN, INT_MAX)); } /* Returns true if the given tree is a BST and its values are >= min and data data > max) return 0; /* otherwise check the subtrees recursively, tightening the min or max constraint */ return isBSTUtil(node->left, min, node->data-1) && // Allow only distinct values isBSTUtil(node->right, node->data+1, max); // Allow only distinct values }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.