Skip to content

Commit 3eda552

Browse files
committedMar 5, 2023
feat: add solutions to lc problem: No.1612
No.1612.Check If Two Expression Trees are Equivalent
1 parent fdb3e05 commit 3eda552

File tree

6 files changed

+392
-197
lines changed

6 files changed

+392
-197
lines changed
 

‎solution/1600-1699/1612.Check If Two Expression Trees are Equivalent/README.md

+161-74
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:递归**
62+
63+
我们定义一个计数器 $cnt$,用于统计每个字母出现的次数。
64+
65+
然后我们分别对两棵二叉表达式树进行深度优先搜索,如果字母出现在左子树,则 $cnt$ 中对应的字母的值加 $1$,如果出现在右子树,则 $cnt$ 中对应的字母的值减 $1$。
66+
67+
最后,我们遍历 $cnt$,如果所有字母的值都为 $0$,则返回 `true`,否则返回 `false`
68+
69+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉表达式树的节点个数。
70+
6171
<!-- tabs:start -->
6272

6373
### **Python3**
@@ -73,18 +83,18 @@
7383
# self.right = right
7484
class Solution:
7585
def checkEquivalence(self, root1: 'Node', root2: 'Node') -> bool:
76-
counter = [0] * 26
77-
78-
def dfs(root, incr):
79-
if root:
80-
dfs(root.left, incr)
81-
dfs(root.right, incr)
82-
if root.val != '+':
83-
counter[ord(root.val) - ord('a')] += incr
84-
86+
def dfs(root, v):
87+
if root is None:
88+
return
89+
if root.val != '+':
90+
cnt[root.val] += v
91+
dfs(root.left, v)
92+
dfs(root.right, v)
93+
94+
cnt = Counter()
8595
dfs(root1, 1)
8696
dfs(root2, -1)
87-
return counter.count(0) == 26
97+
return all(x == 0 for x in cnt.values())
8898
```
8999

90100
```python
@@ -96,23 +106,18 @@ class Solution:
96106
# self.right = right
97107
class Solution:
98108
def checkEquivalence(self, root1: 'Node', root2: 'Node') -> bool:
99-
def calc(ans, left, right, op):
100-
for i in range(26):
101-
if op == '+':
102-
ans[i] = left[i] + right[i]
103-
else:
104-
ans[i] = left[i] - right[i]
105-
106109
def dfs(root):
107-
ans = [0] * 26
108-
if not root:
109-
return ans
110-
if root.val in ['+', '-']:
111-
left, right = dfs(root.left), dfs(root.right)
112-
calc(ans, left, right, root.val)
110+
cnt = [0] * 26
111+
if root is None:
112+
return cnt
113+
if root.val in '+-':
114+
l, r = dfs(root.left), dfs(root.right)
115+
k = 1 if root.val == '+' else -1
116+
for i in range(26):
117+
cnt[i] += l[i] + r[i] * k
113118
else:
114-
ans[ord(root.val) - ord('a')] += 1
115-
return ans
119+
cnt[ord(root.val) - ord('a')] += 1
120+
return cnt
116121

117122
return dfs(root1) == dfs(root2)
118123
```
@@ -138,29 +143,28 @@ class Solution:
138143
* }
139144
*/
140145
class Solution {
141-
private int[] counter;
146+
private int[] cnt = new int[26];
142147

143148
public boolean checkEquivalence(Node root1, Node root2) {
144-
counter = new int[26];
145149
dfs(root1, 1);
146150
dfs(root2, -1);
147-
for (int n : counter) {
148-
if (n != 0) {
151+
for (int x : cnt) {
152+
if (x != 0) {
149153
return false;
150154
}
151155
}
152156
return true;
153157
}
154158

155-
private void dfs(Node root, int incr) {
159+
private void dfs(Node root, int v) {
156160
if (root == null) {
157161
return;
158162
}
159-
dfs(root.left, incr);
160-
dfs(root.right, incr);
161163
if (root.val != '+') {
162-
counter[root.val - 'a'] += incr;
164+
cnt[root.val - 'a'] += v;
163165
}
166+
dfs(root.left, v);
167+
dfs(root.right, v);
164168
}
165169
}
166170
```
@@ -183,35 +187,32 @@ class Solution {
183187
*/
184188
class Solution {
185189
public boolean checkEquivalence(Node root1, Node root2) {
186-
int[] ans1 = dfs(root1);
187-
int[] ans2 = dfs(root2);
190+
int[] cnt1 = dfs(root1);
191+
int[] cnt2 = dfs(root2);
188192
for (int i = 0; i < 26; ++i) {
189-
if (ans1[i] != ans2[i]) {
193+
if (cnt1[i] != cnt2[i]) {
190194
return false;
191195
}
192196
}
193197
return true;
194198
}
195199

196200
private int[] dfs(Node root) {
197-
int[] ans = new int[26];
201+
int[] cnt = new int[26];
198202
if (root == null) {
199-
return ans;
203+
return cnt;
200204
}
201205
if (root.val == '+' || root.val == '-') {
202-
int[] left = dfs(root.left);
203-
int[] right = dfs(root.right);
204-
calc(ans, left, right, root.val);
206+
int[] l = dfs(root.left);
207+
int[] r = dfs(root.right);
208+
int k = root.val == '+' ? 1 : -1;
209+
for (int i = 0; i < 26; ++i) {
210+
cnt[i] += l[i] + r[i] * k;
211+
}
205212
} else {
206-
++ans[root.val - 'a'];
207-
}
208-
return ans;
209-
}
210-
211-
private void calc(int[] ans, int[] left, int[] right, char op) {
212-
for (int i = 0; i < 26; ++i) {
213-
ans[i] = op == '+' ? left[i] + right[i] : left[i] - right[i];
213+
cnt[root.val - 'a']++;
214214
}
215+
return cnt;
215216
}
216217
}
217218
```
@@ -232,20 +233,26 @@ class Solution {
232233
*/
233234
class Solution {
234235
public:
235-
vector<int> counter;
236-
237236
bool checkEquivalence(Node* root1, Node* root2) {
238-
counter.resize(26);
237+
int cnt[26]{};
238+
function<void(Node*, int)> dfs = [&](Node* root, int v) {
239+
if (!root) {
240+
return;
241+
}
242+
if (root->val != '+') {
243+
cnt[root->val - 'a'] += v;
244+
}
245+
dfs(root->left, v);
246+
dfs(root->right, v);
247+
};
239248
dfs(root1, 1);
240249
dfs(root2, -1);
241-
return count(counter.begin(), counter.end(), 0) == 26;
242-
}
243-
244-
void dfs(Node* root, int incr) {
245-
if (!root) return;
246-
dfs(root->left, incr);
247-
dfs(root->right, incr);
248-
if (root->val != '+') counter[root->val - 'a'] += incr;
250+
for (int& x : cnt) {
251+
if (x) {
252+
return false;
253+
}
254+
}
255+
return true;
249256
}
250257
};
251258
```
@@ -265,27 +272,107 @@ public:
265272
class Solution {
266273
public:
267274
bool checkEquivalence(Node* root1, Node* root2) {
275+
function<vector<int>(Node*)> dfs = [&](Node* root) -> vector<int> {
276+
vector<int> cnt(26);
277+
if (!root) {
278+
return cnt;
279+
}
280+
if (root->val == '+' || root->val == '-') {
281+
auto l = dfs(root->left);
282+
auto r = dfs(root->right);
283+
int k = root->val == '+' ? 1 : -1;
284+
for (int i = 0; i < 26; ++i) {
285+
cnt[i] += l[i] + r[i] * k;
286+
}
287+
} else {
288+
cnt[root->val - 'a']++;
289+
}
290+
return cnt;
291+
};
268292
return dfs(root1) == dfs(root2);
269293
}
294+
};
295+
```
296+
297+
### **JavaScript**
270298

271-
vector<int> dfs(Node* root) {
272-
vector<int> ans(26);
273-
if (!root) return ans;
274-
if (root->val == '+' || root->val == '-')
275-
{
276-
auto left = dfs(root->left);
277-
auto right = dfs(root->right);
278-
calc(ans, left, right, root->val);
279-
return ans;
299+
```js
300+
/**
301+
* Definition for a binary tree node.
302+
* function Node(val, left, right) {
303+
* this.val = (val===undefined ? " " : val)
304+
* this.left = (left===undefined ? null : left)
305+
* this.right = (right===undefined ? null : right)
306+
* }
307+
*/
308+
/**
309+
* @param {Node} root1
310+
* @param {Node} root2
311+
* @return {boolean}
312+
*/
313+
var checkEquivalence = function (root1, root2) {
314+
const cnt = new Array(26).fill(0);
315+
const dfs = (root, v) => {
316+
if (!root) {
317+
return;
318+
}
319+
if (root.val !== '+') {
320+
cnt[root.val.charCodeAt(0) - 'a'.charCodeAt(0)] += v;
321+
}
322+
dfs(root.left, v);
323+
dfs(root.right, v);
324+
};
325+
dfs(root1, 1);
326+
dfs(root2, -1);
327+
for (const x of cnt) {
328+
if (x) {
329+
return false;
280330
}
281-
++ans[root->val - 'a'];
282-
return ans;
283331
}
332+
return true;
333+
};
334+
```
284335

285-
void calc(vector<int>& ans, vector<int>& left, vector<int>& right, char op) {
286-
for (int i = 0; i < 26; ++i)
287-
ans[i] = op == '+' ? left[i] + right[i] : left[i] - right[i];
336+
```js
337+
/**
338+
* Definition for a binary tree node.
339+
* function Node(val, left, right) {
340+
* this.val = (val===undefined ? " " : val)
341+
* this.left = (left===undefined ? null : left)
342+
* this.right = (right===undefined ? null : right)
343+
* }
344+
*/
345+
/**
346+
* @param {Node} root1
347+
* @param {Node} root2
348+
* @return {boolean}
349+
*/
350+
var checkEquivalence = function (root1, root2) {
351+
const dfs = root => {
352+
const cnt = new Array(26).fill(0);
353+
if (!root) {
354+
return cnt;
355+
}
356+
if (root.val === '+' || root.val === '-') {
357+
const l = dfs(root.left);
358+
const r = dfs(root.right);
359+
const k = root.val === '+' ? 1 : -1;
360+
for (let i = 0; i < 26; ++i) {
361+
cnt[i] = l[i] + k * r[i];
362+
}
363+
} else {
364+
cnt[root.val.charCodeAt(0) - 'a'.charCodeAt(0)]++;
365+
}
366+
return cnt;
367+
};
368+
const cnt1 = dfs(root1);
369+
const cnt2 = dfs(root2);
370+
for (let i = 0; i < 26; ++i) {
371+
if (cnt1[i] !== cnt2[i]) {
372+
return false;
373+
}
288374
}
375+
return true;
289376
};
290377
```
291378

‎solution/1600-1699/1612.Check If Two Expression Trees are Equivalent/README_EN.md

+151-74
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,18 @@
6464
# self.right = right
6565
class Solution:
6666
def checkEquivalence(self, root1: 'Node', root2: 'Node') -> bool:
67-
counter = [0] * 26
68-
69-
def dfs(root, incr):
70-
if root:
71-
dfs(root.left, incr)
72-
dfs(root.right, incr)
73-
if root.val != '+':
74-
counter[ord(root.val) - ord('a')] += incr
75-
67+
def dfs(root, v):
68+
if root is None:
69+
return
70+
if root.val != '+':
71+
cnt[root.val] += v
72+
dfs(root.left, v)
73+
dfs(root.right, v)
74+
75+
cnt = Counter()
7676
dfs(root1, 1)
7777
dfs(root2, -1)
78-
return counter.count(0) == 26
78+
return all(x == 0 for x in cnt.values())
7979
```
8080

8181
```python
@@ -87,23 +87,18 @@ class Solution:
8787
# self.right = right
8888
class Solution:
8989
def checkEquivalence(self, root1: 'Node', root2: 'Node') -> bool:
90-
def calc(ans, left, right, op):
91-
for i in range(26):
92-
if op == '+':
93-
ans[i] = left[i] + right[i]
94-
else:
95-
ans[i] = left[i] - right[i]
96-
9790
def dfs(root):
98-
ans = [0] * 26
99-
if not root:
100-
return ans
101-
if root.val in ['+', '-']:
102-
left, right = dfs(root.left), dfs(root.right)
103-
calc(ans, left, right, root.val)
91+
cnt = [0] * 26
92+
if root is None:
93+
return cnt
94+
if root.val in '+-':
95+
l, r = dfs(root.left), dfs(root.right)
96+
k = 1 if root.val == '+' else -1
97+
for i in range(26):
98+
cnt[i] += l[i] + r[i] * k
10499
else:
105-
ans[ord(root.val) - ord('a')] += 1
106-
return ans
100+
cnt[ord(root.val) - ord('a')] += 1
101+
return cnt
107102

108103
return dfs(root1) == dfs(root2)
109104
```
@@ -127,29 +122,28 @@ class Solution:
127122
* }
128123
*/
129124
class Solution {
130-
private int[] counter;
125+
private int[] cnt = new int[26];
131126

132127
public boolean checkEquivalence(Node root1, Node root2) {
133-
counter = new int[26];
134128
dfs(root1, 1);
135129
dfs(root2, -1);
136-
for (int n : counter) {
137-
if (n != 0) {
130+
for (int x : cnt) {
131+
if (x != 0) {
138132
return false;
139133
}
140134
}
141135
return true;
142136
}
143137

144-
private void dfs(Node root, int incr) {
138+
private void dfs(Node root, int v) {
145139
if (root == null) {
146140
return;
147141
}
148-
dfs(root.left, incr);
149-
dfs(root.right, incr);
150142
if (root.val != '+') {
151-
counter[root.val - 'a'] += incr;
143+
cnt[root.val - 'a'] += v;
152144
}
145+
dfs(root.left, v);
146+
dfs(root.right, v);
153147
}
154148
}
155149
```
@@ -172,35 +166,32 @@ class Solution {
172166
*/
173167
class Solution {
174168
public boolean checkEquivalence(Node root1, Node root2) {
175-
int[] ans1 = dfs(root1);
176-
int[] ans2 = dfs(root2);
169+
int[] cnt1 = dfs(root1);
170+
int[] cnt2 = dfs(root2);
177171
for (int i = 0; i < 26; ++i) {
178-
if (ans1[i] != ans2[i]) {
172+
if (cnt1[i] != cnt2[i]) {
179173
return false;
180174
}
181175
}
182176
return true;
183177
}
184178

185179
private int[] dfs(Node root) {
186-
int[] ans = new int[26];
180+
int[] cnt = new int[26];
187181
if (root == null) {
188-
return ans;
182+
return cnt;
189183
}
190184
if (root.val == '+' || root.val == '-') {
191-
int[] left = dfs(root.left);
192-
int[] right = dfs(root.right);
193-
calc(ans, left, right, root.val);
185+
int[] l = dfs(root.left);
186+
int[] r = dfs(root.right);
187+
int k = root.val == '+' ? 1 : -1;
188+
for (int i = 0; i < 26; ++i) {
189+
cnt[i] += l[i] + r[i] * k;
190+
}
194191
} else {
195-
++ans[root.val - 'a'];
196-
}
197-
return ans;
198-
}
199-
200-
private void calc(int[] ans, int[] left, int[] right, char op) {
201-
for (int i = 0; i < 26; ++i) {
202-
ans[i] = op == '+' ? left[i] + right[i] : left[i] - right[i];
192+
cnt[root.val - 'a']++;
203193
}
194+
return cnt;
204195
}
205196
}
206197
```
@@ -221,20 +212,26 @@ class Solution {
221212
*/
222213
class Solution {
223214
public:
224-
vector<int> counter;
225-
226215
bool checkEquivalence(Node* root1, Node* root2) {
227-
counter.resize(26);
216+
int cnt[26]{};
217+
function<void(Node*, int)> dfs = [&](Node* root, int v) {
218+
if (!root) {
219+
return;
220+
}
221+
if (root->val != '+') {
222+
cnt[root->val - 'a'] += v;
223+
}
224+
dfs(root->left, v);
225+
dfs(root->right, v);
226+
};
228227
dfs(root1, 1);
229228
dfs(root2, -1);
230-
return count(counter.begin(), counter.end(), 0) == 26;
231-
}
232-
233-
void dfs(Node* root, int incr) {
234-
if (!root) return;
235-
dfs(root->left, incr);
236-
dfs(root->right, incr);
237-
if (root->val != '+') counter[root->val - 'a'] += incr;
229+
for (int& x : cnt) {
230+
if (x) {
231+
return false;
232+
}
233+
}
234+
return true;
238235
}
239236
};
240237
```
@@ -254,27 +251,107 @@ public:
254251
class Solution {
255252
public:
256253
bool checkEquivalence(Node* root1, Node* root2) {
254+
function<vector<int>(Node*)> dfs = [&](Node* root) -> vector<int> {
255+
vector<int> cnt(26);
256+
if (!root) {
257+
return cnt;
258+
}
259+
if (root->val == '+' || root->val == '-') {
260+
auto l = dfs(root->left);
261+
auto r = dfs(root->right);
262+
int k = root->val == '+' ? 1 : -1;
263+
for (int i = 0; i < 26; ++i) {
264+
cnt[i] += l[i] + r[i] * k;
265+
}
266+
} else {
267+
cnt[root->val - 'a']++;
268+
}
269+
return cnt;
270+
};
257271
return dfs(root1) == dfs(root2);
258272
}
273+
};
274+
```
275+
276+
### **JavaScript**
259277

260-
vector<int> dfs(Node* root) {
261-
vector<int> ans(26);
262-
if (!root) return ans;
263-
if (root->val == '+' || root->val == '-')
264-
{
265-
auto left = dfs(root->left);
266-
auto right = dfs(root->right);
267-
calc(ans, left, right, root->val);
268-
return ans;
278+
```js
279+
/**
280+
* Definition for a binary tree node.
281+
* function Node(val, left, right) {
282+
* this.val = (val===undefined ? " " : val)
283+
* this.left = (left===undefined ? null : left)
284+
* this.right = (right===undefined ? null : right)
285+
* }
286+
*/
287+
/**
288+
* @param {Node} root1
289+
* @param {Node} root2
290+
* @return {boolean}
291+
*/
292+
var checkEquivalence = function (root1, root2) {
293+
const cnt = new Array(26).fill(0);
294+
const dfs = (root, v) => {
295+
if (!root) {
296+
return;
297+
}
298+
if (root.val !== '+') {
299+
cnt[root.val.charCodeAt(0) - 'a'.charCodeAt(0)] += v;
300+
}
301+
dfs(root.left, v);
302+
dfs(root.right, v);
303+
};
304+
dfs(root1, 1);
305+
dfs(root2, -1);
306+
for (const x of cnt) {
307+
if (x) {
308+
return false;
269309
}
270-
++ans[root->val - 'a'];
271-
return ans;
272310
}
311+
return true;
312+
};
313+
```
273314

274-
void calc(vector<int>& ans, vector<int>& left, vector<int>& right, char op) {
275-
for (int i = 0; i < 26; ++i)
276-
ans[i] = op == '+' ? left[i] + right[i] : left[i] - right[i];
315+
```js
316+
/**
317+
* Definition for a binary tree node.
318+
* function Node(val, left, right) {
319+
* this.val = (val===undefined ? " " : val)
320+
* this.left = (left===undefined ? null : left)
321+
* this.right = (right===undefined ? null : right)
322+
* }
323+
*/
324+
/**
325+
* @param {Node} root1
326+
* @param {Node} root2
327+
* @return {boolean}
328+
*/
329+
var checkEquivalence = function (root1, root2) {
330+
const dfs = root => {
331+
const cnt = new Array(26).fill(0);
332+
if (!root) {
333+
return cnt;
334+
}
335+
if (root.val === '+' || root.val === '-') {
336+
const l = dfs(root.left);
337+
const r = dfs(root.right);
338+
const k = root.val === '+' ? 1 : -1;
339+
for (let i = 0; i < 26; ++i) {
340+
cnt[i] = l[i] + k * r[i];
341+
}
342+
} else {
343+
cnt[root.val.charCodeAt(0) - 'a'.charCodeAt(0)]++;
344+
}
345+
return cnt;
346+
};
347+
const cnt1 = dfs(root1);
348+
const cnt2 = dfs(root2);
349+
for (let i = 0; i < 26; ++i) {
350+
if (cnt1[i] !== cnt2[i]) {
351+
return false;
352+
}
277353
}
354+
return true;
278355
};
279356
```
280357

‎solution/1600-1699/1612.Check If Two Expression Trees are Equivalent/Solution.cpp

+17-18
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,23 @@
1212
class Solution {
1313
public:
1414
bool checkEquivalence(Node* root1, Node* root2) {
15+
function<vector<int>(Node*)> dfs = [&](Node* root) -> vector<int> {
16+
vector<int> cnt(26);
17+
if (!root) {
18+
return cnt;
19+
}
20+
if (root->val == '+' || root->val == '-') {
21+
auto l = dfs(root->left);
22+
auto r = dfs(root->right);
23+
int k = root->val == '+' ? 1 : -1;
24+
for (int i = 0; i < 26; ++i) {
25+
cnt[i] += l[i] + r[i] * k;
26+
}
27+
} else {
28+
cnt[root->val - 'a']++;
29+
}
30+
return cnt;
31+
};
1532
return dfs(root1) == dfs(root2);
1633
}
17-
18-
vector<int> dfs(Node* root) {
19-
vector<int> ans(26);
20-
if (!root) return ans;
21-
if (root->val == '+' || root->val == '-') {
22-
auto left = dfs(root->left);
23-
auto right = dfs(root->right);
24-
calc(ans, left, right, root->val);
25-
return ans;
26-
}
27-
++ans[root->val - 'a'];
28-
return ans;
29-
}
30-
31-
void calc(vector<int>& ans, vector<int>& left, vector<int>& right, char op) {
32-
for (int i = 0; i < 26; ++i)
33-
ans[i] = op == '+' ? left[i] + right[i] : left[i] - right[i];
34-
}
3534
};

‎solution/1600-1699/1612.Check If Two Expression Trees are Equivalent/Solution.java

+13-16
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,31 @@
1515
*/
1616
class Solution {
1717
public boolean checkEquivalence(Node root1, Node root2) {
18-
int[] ans1 = dfs(root1);
19-
int[] ans2 = dfs(root2);
18+
int[] cnt1 = dfs(root1);
19+
int[] cnt2 = dfs(root2);
2020
for (int i = 0; i < 26; ++i) {
21-
if (ans1[i] != ans2[i]) {
21+
if (cnt1[i] != cnt2[i]) {
2222
return false;
2323
}
2424
}
2525
return true;
2626
}
2727

2828
private int[] dfs(Node root) {
29-
int[] ans = new int[26];
29+
int[] cnt = new int[26];
3030
if (root == null) {
31-
return ans;
31+
return cnt;
3232
}
3333
if (root.val == '+' || root.val == '-') {
34-
int[] left = dfs(root.left);
35-
int[] right = dfs(root.right);
36-
calc(ans, left, right, root.val);
34+
int[] l = dfs(root.left);
35+
int[] r = dfs(root.right);
36+
int k = root.val == '+' ? 1 : -1;
37+
for (int i = 0; i < 26; ++i) {
38+
cnt[i] += l[i] + r[i] * k;
39+
}
3740
} else {
38-
++ans[root.val - 'a'];
39-
}
40-
return ans;
41-
}
42-
43-
private void calc(int[] ans, int[] left, int[] right, char op) {
44-
for (int i = 0; i < 26; ++i) {
45-
ans[i] = op == '+' ? left[i] + right[i] : left[i] - right[i];
41+
cnt[root.val - 'a']++;
4642
}
43+
return cnt;
4744
}
4845
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
var checkEquivalence = function (root1, root2) {
15+
const dfs = root => {
16+
const cnt = new Array(26).fill(0);
17+
if (!root) {
18+
return cnt;
19+
}
20+
if (root.val === '+' || root.val === '-') {
21+
const l = dfs(root.left);
22+
const r = dfs(root.right);
23+
const k = root.val === '+' ? 1 : -1;
24+
for (let i = 0; i < 26; ++i) {
25+
cnt[i] = l[i] + k * r[i];
26+
}
27+
} else {
28+
cnt[root.val.charCodeAt(0) - 'a'.charCodeAt(0)]++;
29+
}
30+
return cnt;
31+
};
32+
const cnt1 = dfs(root1);
33+
const cnt2 = dfs(root2);
34+
for (let i = 0; i < 26; ++i) {
35+
if (cnt1[i] !== cnt2[i]) {
36+
return false;
37+
}
38+
}
39+
return true;
40+
};

‎solution/1600-1699/1612.Check If Two Expression Trees are Equivalent/Solution.py

+10-15
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,17 @@
66
# self.right = right
77
class Solution:
88
def checkEquivalence(self, root1: 'Node', root2: 'Node') -> bool:
9-
def calc(ans, left, right, op):
10-
for i in range(26):
11-
if op == '+':
12-
ans[i] = left[i] + right[i]
13-
else:
14-
ans[i] = left[i] - right[i]
15-
169
def dfs(root):
17-
ans = [0] * 26
18-
if not root:
19-
return ans
20-
if root.val in ['+', '-']:
21-
left, right = dfs(root.left), dfs(root.right)
22-
calc(ans, left, right, root.val)
10+
cnt = [0] * 26
11+
if root is None:
12+
return cnt
13+
if root.val in '+-':
14+
l, r = dfs(root.left), dfs(root.right)
15+
k = 1 if root.val == '+' else -1
16+
for i in range(26):
17+
cnt[i] += l[i] + r[i] * k
2318
else:
24-
ans[ord(root.val) - ord('a')] += 1
25-
return ans
19+
cnt[ord(root.val) - ord('a')] += 1
20+
return cnt
2621

2722
return dfs(root1) == dfs(root2)

0 commit comments

Comments
 (0)
Please sign in to comment.