8000 add 701 · sakurazz/Leetcode-1@ca5fd5f · GitHub
[go: up one dir, main page]

Skip to content

Commit ca5fd5f

Browse files
add 701
1 parent a973cb0 commit ca5fd5f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Your ideas/fixes/algorithms are more than welcome!
115115
|706|[Design HashMap](https://leetcode.com/problems/design-hashmap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_706.java) | O(n) | O(n) | |Easy| Design
116116
|705|[Design HashSet](https://leetcode.com/problems/design-hashset/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_705.java) | O(1) | O(n) | |Easy| Design
117117
|704|[Binary Search](https://leetcode.com/problems/binary-search/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_704.java) | O(logn) | O(1) | |Easy| Binary Search
118+
|701|[Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_701.java) | O(n) | O(h) | |Medium | DFS, recursion
118119
|700|[Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_700.java) | O(n) | O(h) | |Easy| recusion, dfs
119120
|699|[Falling Squares](https://leetcode.com/problems/falling-squares/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_699.java) | O(n^2) | O(n) | |Hard | Segment Tree
120121
|698|[Partition to K Equal Sum Subsets](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_698.java) | O(n*(2^n)) | O(2^n) | |Medium | Backtracking
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
/**
6+
* 701. Insert into a Binary Search Tree
7+
*
8+
* Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
9+
* Note 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.
10+
*
11+
* For example,
12+
*
13+
* Given the tree:
14+
* 4
15+
* / \
16+
* 2 7
17+
* / \
18+
* 1 3
19+
* And the value to insert: 5
20+
*
21+
* You can return this binary search tree:
22+
*
23+
* 4
24+
* / \
25+
* 2 7
26+
* / \ /
27+
* 1 3 5
28+
*
29+
* This tree is also valid:
30+
*
31+
* 5
32+
* / \
33+
* 2 7
34+
* / \
35+
* 1 3
36+
* \
37+
* 4
38+
*/
39+
public class _701 {
40+
public static class Solution1 {
41+
public TreeNode insertIntoBST(TreeNode root, int val) {
42+
if (root == null) {
43+
return new TreeNode(val);
44+
}
45+
if (root.val < val) {
46+
root.right = insertIntoBST(root.right, val);
47+
} else {
48+
root.left = insertIntoBST(root.left, val);
49+
}
50+
return root;
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)
0