class Solution {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
ArrayList<Integer> val1List = new ArrayList();
ArrayList<Integer> val2List = new ArrayList();
findLeaves(root1, val1List);
findLeaves(root2, val2List);
if (val1List.size() != val2List.size()) {
return false;
}
for (int i = 0; i < val1List.size(); ++i) {
if (!val1List.get(i).equals(val2List.get(i))) {
return false;
}
}
return true;
}
private void findLeaves(TreeNode node, ArrayList<Integer> list) {
if (node == null) return;
if (node.left == null && node.right == null) {
list.add(node.val);
}
findLeaves(node.left, list);
findLeaves(node.right, list);
}
}