From 5737219a73570107b3bfe0f7557e77f3e7b60d90 Mon Sep 17 00:00:00 2001 From: Yu Nakamura Date: Tue, 21 Apr 2026 10:18:04 +0900 Subject: [PATCH] add files --- 0098-validate-binary-search-tree/memo.md | 11 +++++++++ 0098-validate-binary-search-tree/step1.cpp | 28 ++++++++++++++++++++++ 0098-validate-binary-search-tree/step2.cpp | 28 ++++++++++++++++++++++ 0098-validate-binary-search-tree/step3.cpp | 28 ++++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 0098-validate-binary-search-tree/memo.md create mode 100644 0098-validate-binary-search-tree/step1.cpp create mode 100644 0098-validate-binary-search-tree/step2.cpp create mode 100644 0098-validate-binary-search-tree/step3.cpp diff --git a/0098-validate-binary-search-tree/memo.md b/0098-validate-binary-search-tree/memo.md new file mode 100644 index 0000000..0af37da --- /dev/null +++ b/0098-validate-binary-search-tree/memo.md @@ -0,0 +1,11 @@ +### step1 + +dfsでmin, maxを更新しながら左の葉の方がmaxと同じか大きいか、右の葉の方がminと同じか小さいかでfalseを返すように実装。 + +### step2 + +変更点なし。 + +### step3 + +3回通すまで書き直し。 diff --git a/0098-validate-binary-search-tree/step1.cpp b/0098-validate-binary-search-tree/step1.cpp new file mode 100644 index 0000000..11c30bc --- /dev/null +++ b/0098-validate-binary-search-tree/step1.cpp @@ -0,0 +1,28 @@ +/** + * 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 traverse(TreeNode* root, long long min, long long max) { + if (!root) { + return true; + } + if (root->val >= max || root->val <= min) { + return false; + } + return traverse(root->left, min, root->val) + && traverse(root->right, root->val, max); + } + + bool isValidBST(TreeNode* root) { + return traverse(root, LLONG_MIN, LLONG_MAX); + } +}; diff --git a/0098-validate-binary-search-tree/step2.cpp b/0098-validate-binary-search-tree/step2.cpp new file mode 100644 index 0000000..11c30bc --- /dev/null +++ b/0098-validate-binary-search-tree/step2.cpp @@ -0,0 +1,28 @@ +/** + * 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 traverse(TreeNode* root, long long min, long long max) { + if (!root) { + return true; + } + if (root->val >= max || root->val <= min) { + return false; + } + return traverse(root->left, min, root->val) + && traverse(root->right, root->val, max); + } + + bool isValidBST(TreeNode* root) { + return traverse(root, LLONG_MIN, LLONG_MAX); + } +}; diff --git a/0098-validate-binary-search-tree/step3.cpp b/0098-validate-binary-search-tree/step3.cpp new file mode 100644 index 0000000..11c30bc --- /dev/null +++ b/0098-validate-binary-search-tree/step3.cpp @@ -0,0 +1,28 @@ +/** + * 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 traverse(TreeNode* root, long long min, long long max) { + if (!root) { + return true; + } + if (root->val >= max || root->val <= min) { + return false; + } + return traverse(root->left, min, root->val) + && traverse(root->right, root->val, max); + } + + bool isValidBST(TreeNode* root) { + return traverse(root, LLONG_MIN, LLONG_MAX); + } +};