Check BT is BST

/***
4.5 Implement a function to check if a binary tree is a binary search tree.
***/

#include<iostream>
#include<limits>

using namespace std;

struct node {
int val;
node *left;
node *right;
};

int last = INT_MAX;

bool isBST(node *root) {
  if(root == nullptr) return true;
if(!isBST(root->left)) return false;
if(root->val < last) return false;
last = root->val;
  return isBST(root->right);
}

Comments

Popular posts from this blog

Sudoku

Longest prefix matching - trie

Climbing Stairs