|
| 1 | +--- |
| 2 | +id: insert-into-a-binary-search-tree |
| 3 | +title: Insert into a Binary Search Tree |
| 4 | +sidebar_label: 701. Insert into a Binary Search Tree |
| 5 | + |
| 6 | +tags: |
| 7 | +- Binary Tree |
| 8 | +- BST |
| 9 | +- Tree |
| 10 | + |
| 11 | +description: "This is a solution to the Insert into a Binary Search Tree problem on LeetCode." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | +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. |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +``` |
| 26 | +Input: root = [4,2,7,1,3], val = 5 |
| 27 | +Output: [4,2,7,1,3,5] |
| 28 | +``` |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +**Example 2:** |
| 33 | + |
| 34 | +``` |
| 35 | +Input: root = [40,20,60,10,30,50,70], val = 25 |
| 36 | +Output: [40,20,60,10,30,50,70,null,null,25] |
| 37 | +
|
| 38 | +``` |
| 39 | + |
| 40 | +**Example 3:** |
| 41 | + |
| 42 | +``` |
| 43 | +Input: root = [4,2,7,1,3,null,null,null,null,null,null], val = 5 |
| 44 | +Output: [4,2,7,1,3,5] |
| 45 | +
|
| 46 | +``` |
| 47 | + |
| 48 | + |
| 49 | +### Constraints |
| 50 | + |
| 51 | +Certainly! Here is the Markdown for the given constraints without changing any word or statement: |
| 52 | + |
| 53 | +### Constraints |
| 54 | + |
| 55 | +- The number of nodes in the tree will be in the range `[0, 10^4]`. |
| 56 | +- `-10^8 <= Node.val <= 10^8` |
| 57 | +- All the values `Node.val` are unique. |
| 58 | +- `-10^8 <= val <= 10^8` |
| 59 | +- It's guaranteed that val does not exist in the original BST. |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +## Solutions |
| 65 | + |
| 66 | +```cpp |
| 67 | + TreeNode* insertIntoBST(TreeNode* a, int x) { |
| 68 | + |
| 69 | + if(!a) return new TreeNode(x); |
| 70 | + |
| 71 | + if(x<a->val) |
| 72 | + a->left=insertIntoBST(a->left,x); |
| 73 | + else |
| 74 | + a->right=insertIntoBST(a->right,x); |
| 75 | + |
| 76 | + return a; |
| 77 | + } |
| 78 | +``` |
| 79 | +
|
| 80 | +```java |
| 81 | +class Solution { |
| 82 | + public TreeNode insertIntoBST(TreeNode root, int val) { |
| 83 | + if(root == null){ |
| 84 | + return new TreeNode(val); |
| 85 | + } |
| 86 | +
|
| 87 | + if(val < root.val){ |
| 88 | + root.left = insertIntoBST(root.left, val); |
| 89 | + } |
| 90 | + else{ |
| 91 | + root.right = insertIntoBST(root.right, val); |
| 92 | + } |
| 93 | +
|
| 94 | + return root; |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +```python |
| 100 | +class Solution: |
| 101 | + def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: |
| 102 | + if root is None: return TreeNode(val) |
| 103 | + if root.val > val: root.left = self.insertIntoBST(root.left, val) |
| 104 | + else: root.right = self.insertIntoBST(root.right, val) |
| 105 | + return root |
| 106 | +``` |
| 107 | +### Complexity Analysis |
| 108 | + |
| 109 | +- **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. |
| 110 | +- **Space Complexity:** $O(H)$, due to the recursive calls on the call stack. In the worst case, $H$ can be $N$ for skewed trees. |
0 commit comments