-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
43 lines (41 loc) · 1.06 KB
/
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
35
36
37
38
39
40
41
42
43
/**
* 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 map = {};
traversal(root1, map);
const stack = [root2, ];
while (stack.length) {
const node = stack.pop();
if (node.val !== '+') {
if (!map[node.val]) {
return false;
}
map[node.val]--;
}
node.left && stack.push(node.left);
node.right && stack.push(node.right);
}
return Object.values(map).every(item => item === 0);
};
function traversal (root, map) {
root.left && traversal(root.left, map);
if (root.val !== '+') {
if (!map[root.val]) {
map[root.val] = 1;
} else {
map[root.val]++;
}
}
root.right && traversal(root.right, map);
}