28. Validate Binary Search Tree[어려움]
02 Jul 2020 | Daily Algorithms
/**
* 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) {
return isValidBST(root, NULL, NULL);
}
bool isValidBST(TreeNode* root, TreeNode* minNode, TreeNode* maxNode)
{
if(!root)
return true;
if(minNode && root->val <= minNode->val || maxNode && root->val >= maxNode->val)
{
return false;
}
return isValidBST(root->left,minNode,root) && isValidBST(root->right, root, maxNode); // 동시에 확인
}
};
/**
* 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) {
return isValidBST(root, NULL, NULL);
}
bool isValidBST(TreeNode* root, TreeNode* minNode, TreeNode* maxNode)
{
if(!root)
return true;
if(minNode && root->val <= minNode->val || maxNode && root->val >= maxNode->val)
{
return false;
}
return isValidBST(root->left,minNode,root) && isValidBST(root->right, root, maxNode); // 동시에 확인
}
};
Comments