Leetcode 98. 验证二叉搜索树(DAY 98) ---- Leetcode Hot 100


原题题目


在这里插入图片描述


代码实现(首刷自解 用了vector数组记录)


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool visit(vector<int>& v,TreeNode* root)
    {
        if(!root)   return true;
        if(!visit(v,root->left))    return false;
        if(v.size())    if(v.back() >= root->val)   return false;
        v.emplace_back(root->val);
        return visit(v,root->right);
    }

    bool isValidBST(TreeNode* root) {
        vector<int> v;
        return visit(v,root);
    }
};

代码实现(首刷自解 全局变量记录)


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    long pre = LONG_MIN;
    bool visit(TreeNode* root)
    {
        if(!root)   return true;
        if(!visit(root->left))    return false;
        if(pre >= root->val)    return false;
        pre = root->val;
        return visit(root->right);
    }

    bool isValidBST(TreeNode* root) {
        return visit(root);
    }
};

代码实现(二刷自解 DAY 164 C++)


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool visit(TreeNode* root,long& pre)
    {
        if(!root)   return true;
        bool left = visit(root->left,pre);
        if(root->val <= pre)    return false;
        pre = root->val;
        if(left)    return visit(root->right,pre);
        else    return false;
    }

    bool isValidBST(TreeNode* root) {
        long pre = LONG_MIN;
        return visit(root,pre);
    }
};

代码实现(三刷自解 DAY 286 递归 C++)


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
  long preval = LONG_MIN;
  bool isValidBST(TreeNode* root) {
    if (!root)  return true;
    bool left = isValidBST(root->left);
    bool judge = root->val > preval;
    preval = root->val;

    return left && judge && isValidBST(root->right); 
  }
};

代码实现(三刷自解 DAY 286 迭代 C++)


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
  bool isValidBST(TreeNode* root) {
    TreeNode* ptr = root;
    stack<TreeNode*> stack;
    long pre = LONG_MIN; 

    while (ptr || !stack.empty()) {
      while (ptr) {
        stack.emplace(ptr);
        ptr = ptr->left;
      } 

      ptr = stack.top();
      stack.pop();
      if (pre >= ptr->val) return false;
      pre = ptr->val;
      ptr = ptr->right;
    }

    return true; 
  }
};

代码实现(四刷自解 DAY 7 Golang)


/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */

var preval int
var visit bool
var ret bool

func Visit(root *TreeNode) {
  if root == nil {
    return
  }

  Visit(root.Left)
  if visit == false {
    preval = root.Val
    visit = true
  } else {
    if preval >= root.Val {
      ret = false
    }
    preval = root.Val
  }
  Visit(root.Right)
} 

func isValidBST(root *TreeNode) bool {
  preval, visit, ret = -1, false, true

  Visit(root)
  return ret
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Love 6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值