-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathdonghyeon95.java
60 lines (52 loc) · 1.49 KB
/
donghyeon95.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.LinkedList;
import java.util.Queue;
/**
* 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 Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
// 트리 순회를 한다
// 현재 순회한 값이 다르다면 return false
if (p==null && q==null) return true;
if (p==null && q!=null) return false;
if (p!=null && q==null) return false;
Queue<TreeNode> pQue = new LinkedList<>();
pQue.add(p);
Queue<TreeNode> qQue = new LinkedList<>();
qQue.add(q);
while (!pQue.isEmpty() && !qQue.isEmpty()) {
TreeNode pNode = pQue.poll();
TreeNode qNode = qQue.poll();
if (pNode.val != qNode.val) return false;
if (pNode.left!=null && qNode.left!=null) {
pQue.add(pNode.left);
qQue.add(qNode.left);
} else if (pNode.left==null && qNode.left!=null) {
return false;
} else if (pNode.left!=null && qNode.left==null) {
return false;
}
if (pNode.right!=null && qNode.right!=null) {
pQue.add(pNode.right);
qQue.add(qNode.right);
} else if (pNode.right==null && qNode.right!=null) {
return false;
} else if (pNode.right!=null && qNode.right==null) {
return false;
}
}
return pQue.isEmpty() && qQue.isEmpty();
}
}