zoukankan      html  css  js  c++  java
  • 39. Recover Binary Search Tree && Validate Binary Search Tree

    Recover Binary Search Tree

    OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/

    Two elements of a binary search tree (BST) are swapped by mistake.

    Recover the tree without changing its structure.

    Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?

    思想: Morris traversal.

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    // morris traversal
    /******************************************************************************************/
    /* Inorder Traversal(should get ascending seq.):Analysis:
    case A: If 2 near nodes swapped,then there will be just 1 Inversion Pair.
    case B: If 2 nodes not near swapped,then there will be 2 Inversion Pairs.
    Weather case A or case B, swap the max-value and the min-value of the Inversion Pair(s).*/
    /*****************************************************************************************/
    class Solution {
    public:
        void recoverTree(TreeNode *root) {
            TreeNode *cur, *pre, *node1, *node2;  // node1, node2: Record 2 near nodes
            TreeNode *first, *second;  // Record 2 swapping nodes
            node1 = node2 = first = NULL;
            cur = root;
            while(cur) {
                if(cur->left == NULL) {
                    if(node1 == NULL) node1 = cur;
                    else if(node2 == NULL) node2 = cur;
                    else { node1 = node2; node2 = cur;}
                    cur = cur->right;
                } else {
                    pre = cur->left;
                    while(pre->right && pre->right != cur) pre = pre->right;
                    if(pre->right == NULL) {
                        pre->right = cur;
                        cur = cur->left;
                        continue;
                    } else {
                        pre->right = NULL;
                        if(node2 == NULL) node2 = cur;
                        else {node1 = node2; node2 = cur;}
                        cur = cur->right;
                    }
                }
                if(node1 && node2 && node1->val > node2->val) {
                    if(first == NULL)  first = node1;
                    second = node2;
                }
            }
            // already learn that there exist 2 nodes swapped.
            int t = first->val;
            first->val = second->val;
            second->val = t;
        }
    };
    

    Validate Binary Search Tree

    OJ: https://oj.leetcode.com/problems/validate-binary-search-tree/

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

    Assume a BST is defined as follows:

    • The left subtree of a node contains only nodes with keys less than the node's key.
    • The right subtree of a node contains only nodes with keys greater than the node's key.
    • Both the left and right subtrees must also be binary search trees.

    Thoughts: As I posted on the discuss forum of Leedcode.

    Solution 1 : Preorder traversal

    class Solution {
    public:
    bool isValidBST(TreeNode *root) {
        if(root == NULL) return true;
        TreeNode *pre = NULL, *post = NULL;
        if(root->left) {
            pre = root->left;
            while(pre->right) pre = pre->right;
            if(pre->val >= root->val) return false;
        }
        if(root->right) {
            post = root->right;
            while(post->left) post = post->left;
            if(post->val <= root->val) return false;
        }
        return isValidBST(root->left) && isValidBST(root->right);
    }
    };
    

     Solution 2: Inorder traversal.

    bool isBST(TreeNode *root, int& preV) {
         if(root == NULL) return true;
         bool l = isBST(root->left, preV);
         if(preV != INT_MIN && preV >= root->val) return false;
         preV = root->val;
         bool r = isBST(root->right, preV);
         return l && r;
    }
    class Solution {
    public:
       bool isValidBST(TreeNode *root) {
           int preV = INT_MIN;  // There exists an Assert.  
           return isBST(root, preV);
       }
    };
    

     Solution 3: Morris Traversal.

    class Solution {
    public:
        void recoverTree(TreeNode *root) {
            TreeNode *cur, *tem, *node1, *node2;
            TreeNode *first, *second;
            node1 = node2 = first = NULL;
            cur = root;
            while(cur) {
                if(cur->left == NULL) {
                    if(node1 == NULL) node1 = cur;
                    else if(node2 == NULL) node2 = cur;
                    else { node1 = node2; node2 = cur;}
                    cur = cur->right;
                } else {
                    tem = cur->left;
                    while(tem->right && tem->right != cur) tem = tem->right;
                    if(tem->right == NULL) {
                        tem->right = cur;
                        cur = cur->left;
                        continue;
                    } else {
                        tem->right = NULL;
                        if(node2 == NULL) node2 = cur;
                        else {node1 = node2; node2 = cur;}
                        cur = cur->right;
                    }
                }
                if(node1 && node2 && node1->val > node2->val) {
            
                    if(first == NULL)  first = node1;
                    second = node2;
                }
            }
            int t = first->val;
            first->val = second->val;
            second->val = t;
        }
    };
    
  • 相关阅读:
    学习Python必须要会的知识,在字符串、列表、元组三者之间相互转换的方法
    python字符串中strip() 函数和 split() 函数的详解
    学习Python必须要知道的4个内置函数
    腾讯轻云服务器,如何使用Windows2016、2019
    jenkins 配置git选分支拉取代码
    查看mysql二进制文件(binlog文件)【报错+解决办法】
    apollo源码部署
    Maven镜像仓库替换为阿里云镜像仓库
    gitlab 日志相关
    gitlab本地搭建后用户(默认)头像不显示问题
  • 原文地址:https://www.cnblogs.com/liyangguang1988/p/3940150.html
Copyright © 2011-2022 走看看