-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path1305-all-elements-in-two-binary-search-trees.js
More file actions
48 lines (47 loc) · 1.1 KB
/
1305-all-elements-in-two-binary-search-trees.js
File metadata and controls
48 lines (47 loc) · 1.1 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
/**
* 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)
* }
*/
const traverse = (node, arr) => {
if(node===null) return
traverse(node.left, arr)
arr.push(node.val)
traverse(node.right, arr)
}
/**
* @param {TreeNode} root1
* @param {TreeNode} root2
* @return {number[]}
*/
var getAllElements = function(root1, root2) {
// approach: do a in-order traversal of both trees to generate two
// arrays sorted in asc order. Then merge these two arrays (similar to
// the merge step of merge-sort)
let arr1 = []
let arr2 = []
traverse(root1, arr1)
traverse(root2, arr2)
let i = 0
let j = 0
const res = []
while(i<arr1.length || j<arr2.length) {
if(i>=arr1.length) {
res.push(arr2[j])
j++
} else if(j>=arr2.length) {
res.push(arr1[i])
i++
} else if(arr1[i]<=arr2[j]) {
res.push(arr1[i])
i++
} else {
res.push(arr2[j])
j++
}
}
return res
};