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


沒有留言:

張貼留言

2007 to 2023 HP and Dell Servers Comparison

  HP Gen5 to Gen11  using ChatGPT HP ProLiant Gen Active Years CPU Socket Popular HP CPUs Cores Base Clock Max RAM Capacity Comparable Dell ...