2016年10月11日 星期二

Check whether it is a valid binary search tree or not in 1 line in C++

struct Node {
       int data;
       Node* left;
       Node* right;
}

#include <limits.h>
bool checkBST(Node* root, int min = INT_MIN, int max = INT_MAX) {
       return !root || root->data >= min && root->data <= max
              && checkBST(root->left, min, root->data - 1) && checkBST(root->right, root->data + 1, max);
}


沒有留言:

張貼留言

2023 Promox on Morefine N6000 16GB 512GB

2023 Promox on Morefine N6000 16GB 512GB Software Etcher 100MB (not but can be rufus-4.3.exe 1.4MB) Proxmox VE 7.4 ISO Installer (1st ISO re...