Repository navigation
Expand file tree
/
Copy pathSameTree.java
More file actions
50 lines (49 loc) · 1.41 KB
/
Copy pathSameTree.java
File metadata and controls
50 lines (49 loc) · 1.41 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
49
50
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class SameTree {
public void toList(TreeNode root, TreeNode[] list) {
if (root == null) {
list[0].right = new TreeNode(Integer.MIN_VALUE);
list[0] = list[0].right;
return;
}
if (root.left == null && root.right == null) {
list[0].right = new TreeNode(root.val);
list[0] = list[0].right;
return;
}
list[0].right = new TreeNode(root.val);
list[0] = list[0].right;
toList(root.left, list);
toList(root.right, list);
}
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null)
return true;
TreeNode qList = new TreeNode(0);
TreeNode pList = new TreeNode(0);
TreeNode[] qPointer = {qList};
TreeNode[] pPointer = {pList};
toList(p, pPointer);
toList(q, qPointer);
qList = qList.right;
pList = pList.right;
while (qList != null && pList != null) {
if(qList.val != pList.val)
return false;
qList = qList.right;
pList = pList.right;
}
if (qList != null || pList != null)
return false;
else
return true;
}
}