文章目录
原题题目
代码实现(首刷自解 用了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
}