forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_872.java
30 lines (26 loc) · 884 Bytes
/
_872.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.List;
public class _872 {
public static class Solution1 {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
List<Integer> leaves1 = new ArrayList<>();
List<Integer> leaves2 = new ArrayList<>();
preorder(root1, leaves1);
preorder(root2, leaves2);
return leaves1.equals(leaves2);
}
private void preorder(TreeNode root,
List<Integer> leaves) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
leaves.add(root.val);
}
preorder(root.left, leaves);
preorder(root.right, leaves);
}
}
}