-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall-elements-in-two-binary-search-trees.js
More file actions
53 lines (49 loc) · 1.21 KB
/
all-elements-in-two-binary-search-trees.js
File metadata and controls
53 lines (49 loc) · 1.21 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)
* }
*/
/**
* 1305. 两棵二叉搜索树中的所有元素
* @param {TreeNode} root1
* @param {TreeNode} root2
* @return {number[]}
*/
var getAllElements = function (root1, root2) {
// 中序遍历二叉搜索树 <=> 遍历有序数组 <=> 合并两个有序数组 ( 类似的题目做过好几个了 )
const arr1 = [];
const arr2 = [];
const dfs = (root, arr) => {
if (root) {
dfs(root.left, arr);
arr.push(root.val);
dfs(root.right, arr);
}
};
dfs(root1, arr1);
dfs(root2, arr2);
return merge(arr1, arr2);
};
function merge(arr1, arr2) {
const result = [];
let left1 = 0;
let left2 = 0;
while (left1 < arr1.length && left2 < arr2.length) {
if (arr1[left1] < arr2[left2]) {
result.push(arr1[left1]);
left1++;
} else {
result.push(arr2[left2]);
left2++;
}
}
if (left1 >= arr1.length) {
result.push(...arr2.slice(left2));
} else {
result.push(...arr1.slice(left1));
}
return result;
}