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);
}


沒有留言:

張貼留言

202501 Debian USB LAN Card Bridge

 202501 Debian USB LAN Card Bridge ChatGPT Question I have a machine running debian 12 with a LAN port using a cable to connect to my office...