-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path701.insert-into-a-binary-search-tree.java
More file actions
135 lines (122 loc) · 3.11 KB
/
Copy path701.insert-into-a-binary-search-tree.java
File metadata and controls
135 lines (122 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* @lc app=leetcode id=701 lang=java
*
* [701] Insert into a Binary Search Tree
*
* https://leetcode.com/problems/insert-into-a-binary-search-tree/description/
*
* algorithms
* Medium (76.64%)
* Total Accepted: 84.5K
* Total Submissions: 107.4K
* Testcase Example: '[4,2,7,1,3]\n5'
*
* 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.
*
* 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.
*
* For example,
*
*
* Given the tree:
* 4
* / \
* 2 7
* / \
* 1 3
* And the value to insert: 5
*
*
* You can return this binary search tree:
*
*
* 4
* / \
* 2 7
* / \ /
* 1 3 5
*
*
* This tree is also valid:
*
*
* 5
* / \
* 2 7
* / \
* 1 3
* \
* 4
*
*
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
/** Rebuild the tree */
public TreeNode insertIntoBST1(TreeNode root, int val) {
List<Integer> inorder = new ArrayList<>();
traverse(root, val, inorder);
if (inorder.get(0) > val) {
inorder.add(0, val);
}
if (inorder.get(inorder.size() - 1) < val) {
inorder.add(val);
}
for (int i = 0; i < inorder.size() - 1; i++) {
if (inorder.get(i) < val && inorder.get(i + 1) > val) {
inorder.add(i + 1, val);
}
}
return buildTree(inorder, 0, inorder.size() - 1);
}
private TreeNode buildTree(List<Integer> inorder, int start, int end) {
if (start > end) {
return null;
}
int mid = start + (end - start) / 2;
TreeNode node = new TreeNode(inorder.get(mid));
node.left = buildTree(inorder, start, mid - 1);
node.right = buildTree(inorder, mid + 1, end);
return node;
}
private void traverse(TreeNode root, int val, List<Integer> inorder) {
if (root == null) {
return;
}
traverse(root.left, val, inorder);
inorder.add(root.val);
traverse(root.right, val, inorder);
}
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) {
return new TreeNode(val);
}
if (root.val > val) {
root.left = insertIntoBST(root.left, val);
} else {
root.right = insertIntoBST(root.right, val);
}
return root;
}
}