diff --git a/dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md b/dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md index 30a0f1009..4ab22172d 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md +++ b/dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md @@ -313,4 +313,4 @@ Here's a step-by-step algorithm for generating all possible letter combinations - Call the backtracking function with the initial index set to 0 and an empty string as the initial combination. - Return the list of combinations. -This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking. +This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking. \ No newline at end of file diff --git a/dsa-solutions/lc-solutions/0000-0099/0098-validate-binary-search-tree.md b/dsa-solutions/lc-solutions/0000-0099/0098-validate-binary-search-tree.md new file mode 100644 index 000000000..c2828e3f4 --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0098-validate-binary-search-tree.md @@ -0,0 +1,95 @@ +--- +id: validate-binary-search-tree +title: Validate Binary Search Tree +sidebar_label: 0098 Validate Binary Search Tree +tags: + - tree + - tree traversal + - LeetCode + - C++ +description: "This is a solution to the Validate Binary Search Tree problem on LeetCode." +--- + +## Problem Description + +Given the root of a binary tree, determine if it is a valid binary search tree (BST). + +A valid BST is defined as follows: + +* The left subtree of a node contains only nodes with keys less than the node's key. +* The right subtree of a node contains only nodes with keys greater than the node's key. +* Both the left and right subtrees must also be binary search trees. + +### Examples + +**Example 1:** + +``` + +Input: root = [2,1,3] +Output: true +``` + +**Example 2:** + +``` +Input: root = [5,1,4,null,null,3,6] +Output: false +``` + +### Constraints + +- The number of nodes in the tree is in the range $[1, 10^4]$. +- $-2^(31) \leq \text{Node.val} \leq 2^(31) - 1$. + +### Approach + +To solve this problem(valid BST) we will do the inorder traversal of the tree as we know in a binary search tree the array of elements which we get after inorder traversal they will be in sorted order so after the inorder traversal we will just have check if the inorder traversal of the given binary search is in sorted order or not. + +#### Code in C++ + +```cpp +/** + * 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: + // Function for inorder traversal + void inorder(TreeNode* root , vector&a){ + if(root==NULL){ + return; + } + inorder(root->left,a); + a.push_back(root->val); + inorder(root->right,a); + } + bool isValidBST(TreeNode* root) { + vectora,b; + inorder(root,a); + b=a; + for(int i=1;iright; + root->right=root->left; + root->left=t; + invert(root->left); + invert(root->right); + } + TreeNode* invertTree(TreeNode* root) { + invert(root); + return root; + + } +}; +``` + + diff --git a/dsa-solutions/lc-solutions/0200-0299/0230-Kth-smallest-element-in-BST.md b/dsa-solutions/lc-solutions/0200-0299/0230-Kth-smallest-element-in-BST.md new file mode 100644 index 000000000..d963cc57e --- /dev/null +++ b/dsa-solutions/lc-solutions/0200-0299/0230-Kth-smallest-element-in-BST.md @@ -0,0 +1,78 @@ +--- +id: kth-smallest-element-in-binary-search-tree +title: Kth Smallest Element in Binary Search Tree +sidebar_label: 0230 Kth Smallest Element in Binary Search Tree +tags: + - tree + - tree traversal + - LeetCode + - C++ +description: "This is a solution to theKth Smallest Element in Binary Search Tree problem on LeetCode." +--- + +## Problem Description + +Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree. + +### Examples + +**Example 1:** + +``` + +Input: root = [3,1,4,null,2], k = 1 +Output: 1 +``` + +**Example 2:** + +``` +Input: root = [5,3,6,2,4,null,null,1], k = 3 +Output: 3 +``` + +### Constraints + +- The number of nodes in the tree is n. +- $1 \leq k \leq n \leq 10^4$ +- $0 \leq \text{Node.val} \leq 10^4$ + +### Approach + +To solve this problem(Kth smallest element in BST) we will do the inorder traversal of the tree as we know in a binary search tree the array of elements which we get after inorder traversal they will be in sorted order so the kth smallest element in the given BST will be the kth index(1-indexed) element in inorder traversal of the BST. + +#### Code in C++ + +```cpp +/** + * 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: + // Function for inorder traversal + void inorder(TreeNode* root,vector&a){ + if(root==NULL){ + return; + } + inorder(root->left,a); + a.push_back(root->val); + inorder(root->right,a); + } + // Function to get the kth smallest element + int kthSmallest(TreeNode* root, int k) { + vectora; + inorder(root,a); + return a[k-1]; // as 0-indexed + } +}; +``` + +