Skip to content

Commit d00c46a

Browse files
authored
Create 1612-check-if-two-expression-trees-are-equivalent.js
1 parent 1751916 commit d00c46a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function Node(val, left, right) {
4+
* this.val = (val===undefined ? " " : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {Node} root1
11+
* @param {Node} root2
12+
* @return {boolean}
13+
*/
14+
const checkEquivalence = function(root1, root2) {
15+
const q = {}
16+
const helper = (node) => {
17+
if (node == null) return
18+
if(node.val !== '+') {
19+
if(q[node.val] == null) q[node.val] = 0
20+
q[node.val]++
21+
}
22+
helper(node.left)
23+
helper(node.right)
24+
}
25+
helper(root1)
26+
const h = node => {
27+
if(node == null) return
28+
if(node.val !== '+') {
29+
if(q[node.val] == null) return false
30+
q[node.val]--
31+
if(q[node.val] <= 0) delete q[node.val]
32+
}
33+
h(node.left)
34+
h(node.right)
35+
}
36+
h(root2)
37+
if(Object.keys(q).length > 0) return false
38+
return true
39+
};

0 commit comments

Comments
 (0)