Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 26 additions & 50 deletions cpp/neetcode_150/07_trees/validate_binary_search_tree.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
/*
Given root of binary tree, determine if it's valid (left all < curr, right all > curr)

Inorder traversal & check if prev >= curr, recursive/iterative solutions

Time: O(n)
Space: O(n)
*/

/**
* Definition for a binary tree node.
* struct TreeNode {
Expand All @@ -18,57 +9,42 @@
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/

// Need a long long int alias because of the constraints of TreeNode.val
#define ll long long int

class Solution {
public:
bool isValidBST(TreeNode* root) {
TreeNode* prev = NULL;
return inorder(root, prev);
return dfs(root, (ll) INT_MIN - 1, (ll) INT_MAX + 1);
}

private:
bool inorder(TreeNode* root, TreeNode*& prev) {
if (root == NULL) {
/**
* DFS through the tree.
*
* Algorithm:
*
* 1. Return true if root is NULL, since it's a balanced binary search tree.
* 2. Checking if root->val is strictly between low and high.
* 3. Updating high value if going to left sub-tree.
* 4. Updating low value if going through right sub-tree.
*
* Time Complexity: O(n) [ Visiting every node exactly once ]
* Space Complexity: O(n) [ Since in worst case, tree is a linked list, so n function calls in call-stack ]
*/
bool dfs(TreeNode* root, ll low, ll high) {

if(root == NULL) {
return true;
}

if (!inorder(root->left, prev)) {
return false;
}

if (prev != NULL && prev->val >= root->val) {
return false;
}
prev = root;

if (!inorder(root->right, prev)) {
if(root->val > low && root->val < high) {
return dfs(root->left, low, (ll) root->val)
&& dfs(root->right, (ll) root->val, high);
} else {
return false;
}

return true;
}
};

// class Solution {
// public:
// bool isValidBST(TreeNode* root) {
// stack<TreeNode*> stk;
// TreeNode* prev = NULL;

// while (!stk.empty() || root != NULL) {
// while (root != NULL) {
// stk.push(root);
// root = root->left;
// }
// root = stk.top();
// stk.pop();

// if (prev != NULL && prev->val >= root->val) {
// return false;
// }

// prev = root;
// root = root->right;
// }

// return true;
// }
// };