forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
40 lines (38 loc) · 878 Bytes
/
Solution.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
/**
* Definition for a binary tree node.
* class Node {
* char val;
* Node left;
* Node right;
* Node() {this.val = ' ';}
* Node(char val) { this.val = val; }
* Node(char val, Node left, Node right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private int[] cnt = new int[26];
public boolean checkEquivalence(Node root1, Node root2) {
dfs(root1, 1);
dfs(root2, -1);
for (int x : cnt) {
if (x != 0) {
return false;
}
}
return true;
}
private void dfs(Node root, int v) {
if (root == null) {
return;
}
if (root.val != '+') {
cnt[root.val - 'a'] += v;
}
dfs(root.left, v);
dfs(root.right, v);
}
}