forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1305.java
35 lines (30 loc) · 1.03 KB
/
_1305.java
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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class _1305 {
public static class Solution1 {
public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
List<Integer> list1 = getAllNodes(root1);
List<Integer> list2 = getAllNodes(root2);
List<Integer> merged = new ArrayList<>();
merged.addAll(list1);
merged.addAll(list2);
Collections.sort(merged);
return merged;
}
private List<Integer> getAllNodes(TreeNode root) {
List<Integer> list = new ArrayList<>();
return inorder(root, list);
}
List<Integer> inorder(TreeNode root, List<Integer> result) {
if (root == null) {
return result;
}
inorder(root.left, result);
result.add(root.val);
return inorder(root.right, result);
}
}
}