-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode872.java
More file actions
48 lines (42 loc) · 1.31 KB
/
leetcode872.java
File metadata and controls
48 lines (42 loc) · 1.31 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
48
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class leetcode872 {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
List<Integer> ls1=new ArrayList<>();
List<Integer> ls2=new ArrayList<>();
List<Integer> list1=checkRoot1(root1,ls1);
List<Integer> list2=checkRoot2(root2,ls2);
return list1.equals(list2);
}
private List<Integer> checkRoot1(TreeNode node,List<Integer> list){
if(node.left==null && node.right==null){
list.add(node.val);
return list;
}
if(node.left!=null) checkRoot1(node.left,list);
if(node.right!=null) checkRoot1(node.right,list);
return list;
}
private List<Integer> checkRoot2(TreeNode node,List<Integer> list){
if(node.left==null && node.right==null){
list.add(node.val);
return list;
}
if(node.left!=null) checkRoot2(node.left,list);
if(node.right!=null) checkRoot2(node.right,list);
return list;
}
}