Saturday, January 5, 2013

Validate Binary Search Tree

Problem:
Given a binary tree, determine if it is a valid binary search tree (BST).
Solution:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (root == NULL)
            return true;
        else if (root->left != NULL && root->right != NULL)
            if ((root->val > biggest(root->left)) && (root->val < smallest(root->right))
                && isValidBST(root->left) && isValidBST(root->right) )
                    return true;
             else
                return false;
        else if (root->left != NULL && (root->right == NULL)) {
            if ((root->val > biggest(root->left))
                && isValidBST(root->left))
                    return true;
             else
                return false;
        } else if (root->right != NULL && (root->left == NULL)) {
            if ((root->val < smallest(root->right))
                && isValidBST(root->right))
                    return true;
             else
                return false;
        } else
            return true;
    }
 
    int biggest(TreeNode *root) {
        while (root->right) {
            root = root->right;
        }
        return root->val;
    }
 
    int smallest(TreeNode *root) {
        while (root->left) {
            root = root->left;
        }
        return root->val;
    }
};

====== Another attempt ======
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int max_of(TreeNode * root) {
        while (root -> right != NULL) {
            root = root -> right;
        }
        return root -> val;
    }
 
     int min_of(TreeNode * root) {
        while (root -> left != NULL) {
            root = root -> left;
        }
        return root -> val;
    }
 
    bool isValidBST(TreeNode* root) {
        if (root == NULL) {
            return true;
        }
        if (root -> left != NULL && root -> val <= max_of(root -> left)) {
            return false;
        }
     
        if (root -> right != NULL && root -> val >= min_of(root -> right)) {
            return false;
        }
     
        if (!isValidBST(root -> left) || !isValidBST(root -> right)) {
            return false;
        }
        return true;
    }
};

======= Another efficient ====
 bool isValidBST(TreeNode* root) {
        TreeNode *prev = NULL;
        return isBST(root, prev);
    }
 
    bool isBST(TreeNode *root, TreeNode *& prev) {
        if (root == NULL) {
            return true;
        }
     
        if (!isBST(root -> left, prev)) {
            return false;
        }
        if (prev == NULL) {
            prev = root;
        } else {
            if (root -> val <= prev -> val) {
                return false;
            }
            prev = root;
        }
        return isBST(root -> right, prev);
    }

============= Another attempt ==========
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        int *prev = NULL;
        return inorder(root, prev);
    }
   
    bool inorder(TreeNode *root, int *&prev) {
        if (root == NULL) {
            return true;
        }
        bool left_ans = inorder(root->left, prev);
        if (prev == NULL) {
            prev = new int(root -> val);
        } else {
            if (*prev >= root -> val) {
                return false;
            } else {
                *prev = root -> val;
            }
        }
       
        bool right_ans = inorder(root -> right, prev);
       
        return left_ans && right_ans;
    }
};

No comments:

Post a Comment