• LeetCode_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.
    

      思路:中序遍历的结果正好是二分查找树对应的数据的顺序,所以先中序遍历数据,然后检查遍历的结果是不是递增的

    /**
     * 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 check(vector<int> &a)
        {
            if(a.size() == 1) return true;
            for(int i = 1; i< a.size(); i++)
              if(a[i] <= a[i-1])
                    return false;
            return true;
       }
        bool isValidBST(TreeNode *root) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(root == NULL ) return true ;
            vector<int> inorder ;
            stack<TreeNode *> myS;
           // myS.push(root);
            TreeNode *p = root;
            while(!myS.empty()||p){
               while(p){
                 myS.push(p);
                 p = p->left;
               }
               p = myS.top();myS.pop();
               inorder.push_back(p->val) ;
               p = p->right ;
            }
            
            return check(inorder) ;
            
        }
    };

     改进版本:访问每个数据的时候就进行检查,奇怪的是小数据时时间减小一半,大数据竟然比上面的用时还要多

     bool isValidBST(TreeNode *root) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(root == NULL ) return true ;
    
            stack<TreeNode *> myS;
           // myS.push(root);
            TreeNode *p = root;
            int flag = 0;
            int value ;
            while(!myS.empty()||p){
               while(p){
                 myS.push(p);
                 p = p->left;
               }
               p = myS.top();
               myS.pop();
              
               if(flag){
                   
                  if( p->val <= value)
                      return false;
                   
               }else{
                   
                   flag = 1;
                 
               }
               value  = p->val;
               p = p->right ;
            }
            return true ;
            
        }
    --------------------------------------------------------------------天道酬勤!
  • 相关阅读:
    Centos7 一键脚本离线安装 Docker-18.03.1-ce
    RedHat 6.8 离线安装Docker (rpm包安装)
    extends和implements区别
    chrome浏览器 同站(SameSite)策略 导致 iframe 内嵌页面cookie无法写入
    快速删除node_modules文件夹:rimraf
    Antd中InputNumber组件数字限制小数位数
    慧聪网如何注册
    2020年9月至10月 Splashtop 新功能
    远程桌面软件的5个首要功能
    Splashtop和 TiFlux 宣布建立合作伙伴关系
  • 原文地址:https://www.cnblogs.com/graph/p/3024428.html
Copyright © 2020-2023  润新知