forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1305.go
More file actions
25 lines (23 loc) · 649 Bytes
/
Copy path1305.go
File metadata and controls
25 lines (23 loc) · 649 Bytes
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
func getAllElements(root1 *TreeNode, root2 *TreeNode) []int {
q1, q2, res := []int{}, []int{}, []int{}
inOrder(root1, &q1)
inOrder(root2, &q2)
for len(q1) > 0 || len(q2) > 0 {
if len(q1) == 0 || (len(q2) > 0 && q2[0] <= q1[0]) {
res = append(res, q2[0])
q2 = q2[1:]
} else if len(q2) == 0 || (len(q1) > 0 && q1[0] < q2[0]) {
res = append(res, q1[0])
q1 = q1[1:]
}
}
return res
}
func inOrder(root *TreeNode, q *[]int) {
if root == nil {
return
}
inOrder(root.Left, q)
*q = append(*q, root.Val)
inOrder(root.Right, q)
}