-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert-into-a-binary-search-tree.js
More file actions
53 lines (50 loc) · 1.12 KB
/
insert-into-a-binary-search-tree.js
File metadata and controls
53 lines (50 loc) · 1.12 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
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} val
* @return {TreeNode}
*/
var insertIntoBST = function (root, val) {
// 递归
if (!root) {
return new TreeNode(val);
}
if (val > root.val) {
root.right = insertIntoBST(root.right, val);
} else if (val < root.val) {
root.left = insertIntoBST(root.left, val);
}
return root;
// 迭代法
// return helper(root, val);
};
// 迭代法
function helper(root, val) {
if (!root) return new TreeNode(val);
let curr = root;
let parent = root;
while (curr) {
// 记录待插入节点的父节点
parent = curr;
if (val > curr.val) {
curr = curr.right;
} else {
curr = curr.left;
}
}
const node = new TreeNode(val);
// 比较大小就可以知道插入左边还是右边
if (val > parent.val) {
parent.right = node;
} else {
parent.left = node;
}
return root;
}