From eb99170858a261a4c3faa33c645655a71538bbc6 Mon Sep 17 00:00:00 2001 From: Youngsang Suh <60172447+youngsangsuh@users.noreply.github.com> Date: Fri, 26 May 2023 15:11:05 -0700 Subject: [PATCH 1/2] Create solution.cpp --- Leetcode/98/ysuh/solution.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Leetcode/98/ysuh/solution.cpp diff --git a/Leetcode/98/ysuh/solution.cpp b/Leetcode/98/ysuh/solution.cpp new file mode 100644 index 0000000..9622cc2 --- /dev/null +++ b/Leetcode/98/ysuh/solution.cpp @@ -0,0 +1,29 @@ +/** + * 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 { +private: + long int left_max_ = LONG_MAX; + long int right_min_ = LONG_MIN; + bool treeWalker(TreeNode* parent, long int left_max, long int right_min) { + if(parent == NULL) return true; + if(parent->val >= left_max || parent->val <= right_min){ + return false; + } + return treeWalker(parent->left, parent->val, right_min) && treeWalker(parent->right, left_max ,parent->val); + } + +public: + bool res = true; + bool isValidBST(TreeNode* root) { + return treeWalker(root, left_max_, right_min_); + } +}; From 9f75518e8139e78bcb947d7d13da1b06fc383640 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 26 May 2023 22:14:11 +0000 Subject: [PATCH 2/2] Add README.md file --- Leetcode/98/ysuh/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Leetcode/98/ysuh/README.md diff --git a/Leetcode/98/ysuh/README.md b/Leetcode/98/ysuh/README.md new file mode 100644 index 0000000..b82ae73 --- /dev/null +++ b/Leetcode/98/ysuh/README.md @@ -0,0 +1,12 @@ +## Link +[Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) + +## Topic +- Tree +- Recursion + +## Approach +We shrink the left & right available range while traversing down the tree with recursive call + +## Note +All the leaves at the left should be smaller than the parent and at the right should be greater than the parent.