zoukankan      html  css  js  c++  java
  • 力扣算法题—098验证二叉搜索树

    给定一个二叉树,判断其是否是一个有效的二叉搜索树。

    假设一个二叉搜索树具有如下特征:

    • 节点的左子树只包含小于当前节点的数。
    • 节点的右子树只包含大于当前节点的数。
    • 所有左子树和右子树自身必须也是二叉搜索树。

    示例 1:

    输入:
        2
       / 
      1   3
    输出: true
    

    示例 2:

    输入:
        5
       / 
      1   4
         / 
        3   6
    输出: false
    解释: 输入为: [5,1,4,null,null,3,6]。
         根节点的值为 5 ,但是其右子节点值为 4 。


      1 #include "_000库函数.h"
      2 
      3 #include <queue>
      4 
      5 struct TreeNode {
      6     int val;
      7     TreeNode *left;
      8     TreeNode *right;
      9     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     10 };
     11 
     12 //解法一:
     13 class Solution {
     14 public:
     15     bool isValidBST(TreeNode* root) {
     16         return helper(root);
     17     }
     18 
     19     bool helper(TreeNode* root) {
     20         if (root->left == NULL && root->right == NULL)
     21             return true;
     22         if (root->left == NULL || root->right == NULL)
     23             return false;
     24         if (root->left->val >= root->val || root->right->val <= root->val)
     25             return false;
     26         return helper(root->left);
     27         helper(root->right);
     28     }
     29 };
     30 
     31 
     32 //解法二:
     33 // Recursion without inorder traversal
     34 class Solution {
     35 public:
     36     bool isValidBST(TreeNode* root) {
     37         return isValidBST(root, LONG_MIN, LONG_MAX);
     38     }
     39     bool isValidBST(TreeNode* root, long mn, long mx) {
     40         if (!root) return true;
     41         if (root->val <= mn || root->val >= mx) return false;
     42         return isValidBST(root->left, mn, root->val) && isValidBST(root->right, root->val, mx);
     43     }
     44 };
     45 
     46 
     47 //解法三:中序遍历
     48 // Recursion
     49 class Solution {
     50 public:
     51     bool isValidBST(TreeNode* root) {
     52         if (!root) return true;
     53         vector<int> vals;
     54         inorder(root, vals);
     55         for (int i = 0; i < vals.size() - 1; ++i) {
     56             if (vals[i] >= vals[i + 1]) return false;
     57         }
     58         return true;
     59     }
     60     void inorder(TreeNode* root, vector<int>& vals) {
     61         if (!root) return;
     62         inorder(root->left, vals);
     63         vals.push_back(root->val);
     64         inorder(root->right, vals);
     65     }
     66 };
     67 
     68 //解法4:
     69 class Solution {
     70 public:
     71     bool isValidBST(TreeNode* root) {
     72         if (!root)
     73             return true;
     74         queue<TreeNode* >q;
     75         q.push(root);
     76         helper(q, root);
     77         while (!q.empty()) {
     78             int a = q.front()->val;
     79             q.pop();
     80             int b = q.front()->val;
     81             if (a >= b)
     82                 return false;
     83         }
     84     }
     85     void helper(queue<TreeNode* >&q, TreeNode* root) {
     86         if (!root)
     87             return;
     88         helper(q, root->left);
     89         q.push(root);
     90         helper(q, root->right);
     91     }
     92 };
     93 
     94 
     95 TreeNode* CreateBTree(const vector<int> &v)
     96 {
     97     if (v.size() == 0)
     98         return nullptr;
     99     TreeNode* root = new TreeNode(v[0]);
    100     auto cursor = root;
    101     queue<TreeNode*> que;
    102     que.push(cursor);
    103     for (size_t i = 1; i < v.size(); i++)
    104     {
    105         if (que.empty())
    106             break;
    107         cursor = que.front();
    108         if (cursor == NULL)
    109             break;
    110         que.pop();
    111         
    112         if (v[i] != 0)        
    113             cursor->left = new TreeNode(v[i]);            
    114         if (v[++i] != 0)
    115             cursor->right = new TreeNode(v[i]);
    116 
    117         que.push(cursor->left);
    118         que.push(cursor->right);
    119     }
    120     return root;
    121 }
    122 
    123 
    124 
    125 
    126 void T098() {
    127     Solution s;
    128     vector<int>v = { 6,5,7,0,0,6,8};
    129     TreeNode *root;
    130 
    131     root = CreateBTree(v);
    132     cout << s.isValidBST(root) << endl;
    133 
    134     v = { 2,1,3 };
    135     root = CreateBTree(v);
    136     cout << s.isValidBST(root) << endl;
    137 }
  • 相关阅读:
    InnoDB实现MVCC原理
    Python中定义函数时参数有默认值的小陷阱
    Python系统编程笔记
    Python中的字典
    Python中常见的字符串小笔试题
    Oracle常见名词解析
    Oracle数据库面试题【转载】
    Oracle日期语言修改
    Oracle日期时间函数大全
    Oracle数据库分页的三种方法
  • 原文地址:https://www.cnblogs.com/zzw1024/p/10946689.html
Copyright © 2011-2022 走看看