My solution is also same except I declared prev as class attribute and used same function. Code: class Solution { int prev = -1; public: bool isBST(Node* root) { if(!root) return true; bool left = isBST(root->left); if(!left) return false; if(root->data data; bool right = isBST(root->right); return left && right; } };
Nice ❤
GFG POTD 25/07/2024
Node *buildBSTFromInorder(const vector &inorder, int start, int end)
{
if (start > end)
{
return nullptr;
}
int mid = (start + end) / 2;
Node *node = new Node(inorder[mid]);
node->left = buildBSTFromInorder(inorder, start, mid - 1);
node->right = buildBSTFromInorder(inorder, mid + 1, end);
return node;
}
class Solution {
public:
Node* sortedArrayToBST(vector& nums) {
// Code here
Node *root = buildBSTFromInorder(nums, 0, nums.size() - 1);
return root;
}
};
My solution is also same except I declared prev as class attribute and used same function.
Code:
class Solution {
int prev = -1;
public:
bool isBST(Node* root) {
if(!root) return true;
bool left = isBST(root->left);
if(!left) return false;
if(root->data data;
bool right = isBST(root->right);
return left && right;
}
};