zoukankan      html  css  js  c++  java
  • 【Lintcode】095.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.
    • A single node tree is a BST

    An example:

      2
     / 
    1   4
       / 
      3   5
    

    The above binary tree is serialized as {2,1,4,#,#,3,5} (in level order).

    题解:

      按照定义,二叉搜索树的中序遍历是升序序列

    Solution 1 ()

    class Solution {
    public:
        /**
         * @param root: The root of binary tree.
         * @return: True if the binary tree is BST, or false
         */
        bool isValidBST(TreeNode *root) {
            TreeNode *prev = nullptr;
            return validate(root, prev);
        }
        
        bool validate(TreeNode *node, TreeNode* &prev) {
            if (node == nullptr) {
                return true;
            }
            if (!validate(node->left,prev)) {
                return false;
            }
            if (prev != nullptr && prev->val >= node->val) {
                return false;
            }
            prev = node;
            
            return validate(node->right, prev);
        }
    };

      分治法 from here

    Solution 2 () 

    class ResultType {
    public:
            bool isBST;
            TreeNode *maxNode, *minNode;
            ResultType(): isBST(true), maxNode(nullptr), minNode(nullptr) {}    
    };
    class Solution {
    public:
        
        bool isValidBST(TreeNode *root) {
            ResultType result = helper(root);
            return result.isBST;
        }
        
        ResultType helper(TreeNode *root) {
            ResultType result;
            if (root == nullptr) {
                return result;
            }
            
            ResultType left = helper(root->left);
            ResultType right = helper(root->right);
            
            if (!left.isBST || !right.isBST) {
                result.isBST = false;
                return result;
            }
            if (left.maxNode != nullptr && left.maxNode->val >= root->val) {
                result.isBST = false;
                return result;
            }
            if (right.minNode != nullptr && right.minNode->val <= root->val) {
                result.isBST = false;
                return result;
            }
            
            result.isBST = true;
            result.minNode = left.minNode == nullptr ? root : left.minNode;
            result.maxNode = right.maxNode == nullptr ? root : right.maxNode;
            
            return result;
        }
    };
  • 相关阅读:
    vue-cli-service: command not found
    parted 大容量硬盘分区 格式化
    RAC集群安装错误集合
    印象笔记 Windows 客户端“未响应”怎么办?
    split,splice,slice 三者的用法
    JQuery 2.0.3 源码结构
    数据库字段备注信息声明语法 CDL (Comment Declaration Language)
    mysql底层原理解析(一)之日志
    ES底层原理解析
    aspnetCore 3.1网站部署到IIS
  • 原文地址:https://www.cnblogs.com/Atanisi/p/6834898.html
Copyright © 2011-2022 走看看