Red Hat Interview Question

You are given a binary tree as input , find whether the tree is a BST or not .

Interview Answers

Anonymous

Aug 15, 2013

Take the inorder traversal of the tree . If its a sorted list , its bst .

Anonymous

Feb 19, 2019

bool isBST(Node* root) { if (root == NULL) return true; bool lbst = true; bool rbst = true; if (root->left) { if(root->left->val val) return false; lbst = isBST(root->left); } if (lbst && root->right) { if (root->right->val > root->val) return false; rbst = isBST(root->right); } return rbst && lbst;