Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a9aad79

Browse files
authored
feat: add ts solution to lc problem: No.701 (doocs#2833)
1 parent 4cb7256 commit a9aad79

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

‎solution/0700-0799/0701.Insert into a Binary Search Tree/README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,33 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode {
177177
}
178178
```
179179

180+
#### TypeScript
181+
182+
```ts
183+
/**
184+
* Definition for a binary tree node.
185+
* class TreeNode {
186+
* val: number
187+
* left: TreeNode | null
188+
* right: TreeNode | null
189+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
190+
* this.val = (val===undefined ? 0 : val)
191+
* this.left = (left===undefined ? null : left)
192+
* this.right = (right===undefined ? null : right)
193+
* }
194+
* }
195+
*/
196+
197+
function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null {
198+
if (!root) return new TreeNode(val);
199+
200+
if (val < root.val) root.left = insertIntoBST(root.left, val);
201+
else root.right = insertIntoBST(root.right, val);
202+
203+
return root;
204+
}
205+
```
206+
180207
<!-- tabs:end -->
181208

182209
<!-- solution:end -->

‎solution/0700-0799/0701.Insert into a Binary Search Tree/README_EN.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,33 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode {
175175
}
176176
```
177177

178+
#### TypeScript
179+
180+
```ts
181+
/**
182+
* Definition for a binary tree node.
183+
* class TreeNode {
184+
* val: number
185+
* left: TreeNode | null
186+
* right: TreeNode | null
187+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
188+
* this.val = (val===undefined ? 0 : val)
189+
* this.left = (left===undefined ? null : left)
190+
* this.right = (right===undefined ? null : right)
191+
* }
192+
* }
193+
*/
194+
195+
function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null {
196+
if (!root) return new TreeNode(val);
197+
198+
if (val < root.val) root.left = insertIntoBST(root.left, val);
199+
else root.right = insertIntoBST(root.right, val);
200+
201+
return root;
202+
}
203+
```
204+
178205
<!-- tabs:end -->
179206

180207
<!-- solution:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null {
16+
if (!root) return new TreeNode(val);
17+
18+
if (val < root.val) root.left = insertIntoBST(root.left, val);
19+
else root.right = insertIntoBST(root.right, val);
20+
21+
return root;
22+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /