|
| 1 | +701. 二叉搜索树中的插入操作 |
| 2 | + |
| 3 | +https://leetcode.cn/problems/insert-into-a-binary-search-tree/ |
| 4 | + |
| 5 | +简单递归版本 |
| 6 | + |
| 7 | +```java [] |
| 8 | +class Solution { |
| 9 | + public TreeNode insertIntoBST(TreeNode root, int val) { |
| 10 | + |
| 11 | + if (root == null) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。 |
| 12 | + return new TreeNode(val); |
| 13 | + if (root.val < val){ |
| 14 | + root.right = insertIntoBST(root.right, val); // 递归创建右子树 |
| 15 | + }else if (root.val > val){ |
| 16 | + root.left = insertIntoBST(root.left, val); // 递归创建左子树 |
| 17 | + } |
| 18 | + return root; |
| 19 | + } |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +```kotlin [] |
| 24 | +/** |
| 25 | + * Example: |
| 26 | + * var ti = TreeNode(5) |
| 27 | + * var v = ti.`val` |
| 28 | + * Definition for a binary tree node. |
| 29 | + * class TreeNode(var `val`: Int) { |
| 30 | + * var left: TreeNode? = null |
| 31 | + * var right: TreeNode? = null |
| 32 | + * } |
| 33 | + */ |
| 34 | +class Solution { |
| 35 | + fun insertIntoBST(root: TreeNode?, `val`: Int): TreeNode? { |
| 36 | +if(root==null)return TreeNode(`val`) |
| 37 | +if(root.`val`>`val`)root.left=insertIntoBST(root.left,`val`) |
| 38 | +if(root.`val`<`val`)root.right=insertIntoBST(root.right,`val`) |
| 39 | +return root |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +```typescript [] |
| 45 | +/** |
| 46 | + * Definition for a binary tree node. |
| 47 | + * class TreeNode { |
| 48 | + * val: number |
| 49 | + * left: TreeNode | null |
| 50 | + * right: TreeNode | null |
| 51 | + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { |
| 52 | + * this.val = (val===undefined ? 0 : val) |
| 53 | + * this.left = (left===undefined ? null : left) |
| 54 | + * this.right = (right===undefined ? null : right) |
| 55 | + * } |
| 56 | + * } |
| 57 | + */ |
| 58 | + |
| 59 | +function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null { |
| 60 | + if(root === null) return { val,left:null,right:null } |
| 61 | + if(root.val > val) root.left = insertIntoBST(root.left,val) |
| 62 | + else if(root.val < val) root.right = insertIntoBST(root.right,val); |
| 63 | + return root; |
| 64 | +}; |
| 65 | +``` |
| 66 | + |
| 67 | +```go [] |
| 68 | +func insertIntoBST(root *TreeNode, val int) *TreeNode { |
| 69 | + if root == nil { |
| 70 | + root = &TreeNode{Val: val} |
| 71 | + return root |
| 72 | + } |
| 73 | + if root.Val > val { |
| 74 | + root.Left = insertIntoBST(root.Left, val) |
| 75 | + } else { |
| 76 | + root.Right = insertIntoBST(root.Right, val) |
| 77 | + } |
| 78 | + return root |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +```cpp [] |
| 83 | +class Solution { |
| 84 | +public: |
| 85 | + TreeNode* insertIntoBST(TreeNode* root, int val) { |
| 86 | + if (root == NULL) { |
| 87 | + TreeNode* node = new TreeNode(val); |
| 88 | + return node; |
| 89 | + } |
| 90 | + if (root->val > val) root->left = insertIntoBST(root->left, val); |
| 91 | + if (root->val < val) root->right = insertIntoBST(root->right, val); |
| 92 | + return root; |
| 93 | + } |
| 94 | +}; |
| 95 | +``` |
| 96 | +
|
| 97 | +```php [] |
| 98 | +class Solution |
| 99 | +{ |
| 100 | + // 递归函数,返回构造后 BST 的根节点 |
| 101 | + public function insertIntoBST($root, $val) |
| 102 | + { |
| 103 | + if ($root === null) return new TreeNode($val); |
| 104 | +
|
| 105 | + if ($val < $root->val) { |
| 106 | + $root->left = $this->insertIntoBST($root->left, $val); |
| 107 | + } else { |
| 108 | + $root->right = $this->insertIntoBST($root->right, $val); |
| 109 | + } |
| 110 | + |
| 111 | + return $root; |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +```csharp [] |
| 117 | +/** |
| 118 | + * Definition for a binary tree node. |
| 119 | + * public class TreeNode { |
| 120 | + * public int val; |
| 121 | + * public TreeNode left; |
| 122 | + * public TreeNode right; |
| 123 | + * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { |
| 124 | + * this.val = val; |
| 125 | + * this.left = left; |
| 126 | + * this.right = right; |
| 127 | + * } |
| 128 | + * } |
| 129 | + */ |
| 130 | +public class Solution { |
| 131 | + public TreeNode InsertIntoBST(TreeNode root, int val) { |
| 132 | + |
| 133 | + |
| 134 | + if (root == null) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。 |
| 135 | + return new TreeNode(val); |
| 136 | + if (root.val < val){ |
| 137 | + root.right = InsertIntoBST(root.right, val); // 递归创建右子树 |
| 138 | + }else if (root.val > val){ |
| 139 | + root.left = InsertIntoBST(root.left, val); // 递归创建左子树 |
| 140 | + } |
| 141 | + return root; |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +```python [] |
| 147 | +class Solution: |
| 148 | + def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: |
| 149 | + if root is None: |
| 150 | + return TreeNode(val) # 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。 |
| 151 | + if root.val < val: |
| 152 | + root.right = self.insertIntoBST(root.right, val) # 递归创建右子树 |
| 153 | + if root.val > val: |
| 154 | + root.left = self.insertIntoBST(root.left, val) # 递归创建左子树 |
| 155 | + return root |
| 156 | +``` |
| 157 | + |
| 158 | +```scala [] |
| 159 | +/** |
| 160 | + * Definition for a binary tree node. |
| 161 | + * class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) { |
| 162 | + * var value: Int = _value |
| 163 | + * var left: TreeNode = _left |
| 164 | + * var right: TreeNode = _right |
| 165 | + * } |
| 166 | + */ |
| 167 | +object Solution { |
| 168 | + def insertIntoBST(root: TreeNode, `val`: Int): TreeNode = { |
| 169 | + if (root != null) { |
| 170 | + if (root.value < `val`) { |
| 171 | + root.right = insertIntoBST(root.right, `val`) |
| 172 | + } |
| 173 | + else { |
| 174 | + root.left = insertIntoBST(root.left, `val`) |
| 175 | + } |
| 176 | + } else { |
| 177 | + return new TreeNode(`val`) |
| 178 | + } |
| 179 | + root |
| 180 | + } |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +```swift [] |
| 185 | +class Solution { |
| 186 | + func insertIntoBST(_ root: TreeNode?, _ val: Int) -> TreeNode? { |
| 187 | + if root == nil{return TreeNode(val)} |
| 188 | + if val<root!.val{ |
| 189 | + root?.left=insertIntoBST(root?.left, val) |
| 190 | + }else{ |
| 191 | + root?.right=insertIntoBST(root?.right, val) |
| 192 | + } |
| 193 | + return root |
| 194 | + } |
| 195 | +} |
| 196 | +``` |
0 commit comments