-
-
Notifications
You must be signed in to change notification settings - Fork 156
solution to problem 0701 #3459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
solution to problem 0701 #3459
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eb24a0e
Create 0701-Insert-Into-A-Binary-Search-Tree.md
Tanishashrivas c7b697b
Update 0700-0799.md
Tanishashrivas 030e146
Update 0700-0799.md
Tanishashrivas 9704f97
Merge branch 'main' into issues
Tanishashrivas ec9d66c
Update 0700-0799.md
ajay-dhangar 33e2f5a
Merge branch 'main' into issues
ajay-dhangar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
dsa-solutions/lc-solutions/0700-0799/0701-Insert-Into-A-Binary-Search-Tree.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:** | ||
|
||
 | ||
|
||
``` | ||
Input: root = [4,2,7,1,3], val = 5 | ||
Output: [4,2,7,1,3,5] | ||
``` | ||
 | ||
|
||
|
||
**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(x<a->val) | ||
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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.