forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.js
34 lines (34 loc) · 803 Bytes
/
Solution.js
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
/**
* Definition for a binary tree node.
* function Node(val, left, right) {
* this.val = (val===undefined ? " " : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {Node} root1
* @param {Node} root2
* @return {boolean}
*/
var checkEquivalence = function (root1, root2) {
const cnt = new Array(26).fill(0);
const dfs = (root, v) => {
if (!root) {
return;
}
if (root.val !== '+') {
cnt[root.val.charCodeAt(0) - 'a'.charCodeAt(0)] += v;
}
dfs(root.left, v);
dfs(root.right, v);
};
dfs(root1, 1);
dfs(root2, -1);
for (const x of cnt) {
if (x) {
return false;
}
}
return true;
};