From eb24a0ec42d0997502a89704b04425302d2b015b Mon Sep 17 00:00:00 2001 From: Tanisha Shrivas <114223066+Tanishashrivas@users.noreply.github.com> Date: Wed, 17 Jul 2024 00:27:25 +0530 Subject: [PATCH 1/4] Create 0701-Insert-Into-A-Binary-Search-Tree.md --- .../0701-Insert-Into-A-Binary-Search-Tree.md | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0700-0799/0701-Insert-Into-A-Binary-Search-Tree.md diff --git a/dsa-solutions/lc-solutions/0700-0799/0701-Insert-Into-A-Binary-Search-Tree.md b/dsa-solutions/lc-solutions/0700-0799/0701-Insert-Into-A-Binary-Search-Tree.md new file mode 100644 index 000000000..7c72d91d7 --- /dev/null +++ b/dsa-solutions/lc-solutions/0700-0799/0701-Insert-Into-A-Binary-Search-Tree.md @@ -0,0 +1,110 @@ +--- +id: insert-into-a-binary-search-tree +title: Insert into a Binary Search Tree +sidebar_label: 701. Insert into a Binary Search Tree + +tags: +- Binary Tree +- BST +- Tree + +description: "This is a solution to the Insert into a Binary Search Tree problem on LeetCode." +--- + +## Problem Description +You are given the `root` node of a binary search tree (BST) and a `value` to insert into the tree. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST. + +Notice that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them. + +### Examples + +**Example 1:** + +![Example1](https://github.com/user-attachments/assets/c7df9581-e34c-4cde-a8a3-5b19d37d60c7) + +``` +Input: root = [4,2,7,1,3], val = 5 +Output: [4,2,7,1,3,5] +``` +![Sol1](https://github.com/user-attachments/assets/a454cca0-8878-44e3-8c22-1be2c870cce1) + + +**Example 2:** + +``` +Input: root = [40,20,60,10,30,50,70], val = 25 +Output: [40,20,60,10,30,50,70,null,null,25] + +``` + +**Example 3:** + +``` +Input: root = [4,2,7,1,3,null,null,null,null,null,null], val = 5 +Output: [4,2,7,1,3,5] + +``` + + +### Constraints + +Certainly! Here is the Markdown for the given constraints without changing any word or statement: + +### Constraints + +- The number of nodes in the tree will be in the range `[0, 10^4]`. +- `-10^8 <= Node.val <= 10^8` +- All the values `Node.val` are unique. +- `-10^8 <= val <= 10^8` +- It's guaranteed that val does not exist in the original BST. + + + + +## Solutions + +```cpp + TreeNode* insertIntoBST(TreeNode* a, int x) { + + if(!a) return new TreeNode(x); + + if(xval) + a->left=insertIntoBST(a->left,x); + else + a->right=insertIntoBST(a->right,x); + + return a; + } +``` + +```java +class Solution { + public TreeNode insertIntoBST(TreeNode root, int val) { + if(root == null){ + return new TreeNode(val); + } + + if(val < root.val){ + root.left = insertIntoBST(root.left, val); + } + else{ + root.right = insertIntoBST(root.right, val); + } + + return root; + } +} +``` + +```python +class Solution: + def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: + if root is None: return TreeNode(val) + if root.val > val: root.left = self.insertIntoBST(root.left, val) + else: root.right = self.insertIntoBST(root.right, val) + return root +``` +### Complexity Analysis + +- **Time Complexity:** $O(H)$, where $H$ is the height of the BST. In the worst case, the height $H$ can be $N$ for skewed trees. +- **Space Complexity:** $O(H)$, due to the recursive calls on the call stack. In the worst case, $H$ can be $N$ for skewed trees. From c7b697b70c70d50b509bb6fee0ac4edb2e631000 Mon Sep 17 00:00:00 2001 From: Tanisha Shrivas <114223066+Tanishashrivas@users.noreply.github.com> Date: Wed, 17 Jul 2024 00:28:36 +0530 Subject: [PATCH 2/4] Update 0700-0799.md --- dsa-problems/leetcode-problems/0700-0799.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dsa-problems/leetcode-problems/0700-0799.md b/dsa-problems/leetcode-problems/0700-0799.md index 72201e204..40cfcbdad 100644 --- a/dsa-problems/leetcode-problems/0700-0799.md +++ b/dsa-problems/leetcode-problems/0700-0799.md @@ -20,7 +20,7 @@ export const problems = [ "problemName": "701. Insert into a Binary Search Tree", "difficulty": "Medium", "leetCodeLink": "https://leetcode.com/problems/insert-into-a-binary-search-tree", - "solutionLink": "#" + "solutionLink": "/dsa-solutions/lc-solutions/0700-0799/0701-Insert-Into-A-Binary-Search-Tree" }, { "problemName": "702. Search in a Sorted Array of Unknown Size", @@ -619,4 +619,4 @@ export const problems = [ collectionLink="https://leetcode.com/study-plan/programming-skills" /> -Now, you can see the list of problems in a table format. You can click on the problem link to view the problem on the LeetCode website. You can also click on the solution link to view the solution of the problem. \ No newline at end of file +Now, you can see the list of problems in a table format. You can click on the problem link to view the problem on the LeetCode website. You can also click on the solution link to view the solution of the problem. From 030e146f460491173e1e9b663acd6607dfa47469 Mon Sep 17 00:00:00 2001 From: Tanisha Shrivas <114223066+Tanishashrivas@users.noreply.github.com> Date: Sun, 21 Jul 2024 20:14:32 +0530 Subject: [PATCH 3/4] Update 0700-0799.md --- dsa-problems/leetcode-problems/0700-0799.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-problems/leetcode-problems/0700-0799.md b/dsa-problems/leetcode-problems/0700-0799.md index 40cfcbdad..6fb6aed8a 100644 --- a/dsa-problems/leetcode-problems/0700-0799.md +++ b/dsa-problems/leetcode-problems/0700-0799.md @@ -20,7 +20,7 @@ export const problems = [ "problemName": "701. Insert into a Binary Search Tree", "difficulty": "Medium", "leetCodeLink": "https://leetcode.com/problems/insert-into-a-binary-search-tree", - "solutionLink": "/dsa-solutions/lc-solutions/0700-0799/0701-Insert-Into-A-Binary-Search-Tree" + "solutionLink": "/dsa-solutions/lc-solutions/0700-0799/0701-insert-into-a-binary-search-tree" }, { "problemName": "702. Search in a Sorted Array of Unknown Size", From ec9d66ca85609f6438f0178f6f7a296a4ac7aeb3 Mon Sep 17 00:00:00 2001 From: Ajay Dhangar <99037494+Ajay-Dhangar@users.noreply.github.com> Date: Sun, 21 Jul 2024 22:22:35 +0530 Subject: [PATCH 4/4] Update 0700-0799.md --- dsa-problems/leetcode-problems/0700-0799.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-problems/leetcode-problems/0700-0799.md b/dsa-problems/leetcode-problems/0700-0799.md index 63484a233..0ff1ede5e 100644 --- a/dsa-problems/leetcode-problems/0700-0799.md +++ b/dsa-problems/leetcode-problems/0700-0799.md @@ -20,7 +20,7 @@ export const problems = [ "problemName": "701. Insert into a Binary Search Tree", "difficulty": "Medium", "leetCodeLink": "https://leetcode.com/problems/insert-into-a-binary-search-tree", - "solutionLink": "/dsa-solutions/lc-solutions/0700-0799/0701-insert-into-a-binary-search-tree" + "solutionLink": "/dsa-solutions/lc-solutions/0700-0799/insert-into-a-binary-search-tree" }, { "problemName": "702. Search in a Sorted Array of Unknown Size",