Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 226d730

Browse files
committed
feat: add solutions to lc problem: No.1612
No.1612.Check If Two Expression Trees are Equivalent
1 parent 4972ae2 commit 226d730

File tree

8 files changed

+2511
-22
lines changed

8 files changed

+2511
-22
lines changed

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

Lines changed: 213 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
<li>给定的树<strong>保证</strong>是有效的二叉表达式树。</li>
5353
</ul>
5454

55-
5655
## 解法
5756

5857
<!-- 这里可写通用的实现逻辑 -->
@@ -64,15 +63,228 @@
6463
<!-- 这里可写当前语言的特殊实现逻辑 -->
6564

6665
```python
66+
# Definition for a binary tree node.
67+
# class Node(object):
68+
# def __init__(self, val=" ", left=None, right=None):
69+
# self.val = val
70+
# self.left = left
71+
# self.right = right
72+
class Solution:
73+
def checkEquivalence(self, root1: 'Node', root2: 'Node') -> bool:
74+
counter = [0] * 26
75+
76+
def dfs(root, incr):
77+
if root:
78+
dfs(root.left, incr)
79+
dfs(root.right, incr)
80+
if root.val != '+':
81+
counter[ord(root.val) - ord('a')] += incr
82+
83+
dfs(root1, 1)
84+
dfs(root2, -1)
85+
return counter.count(0) == 26
86+
```
6787

88+
```python
89+
# Definition for a binary tree node.
90+
# class Node(object):
91+
# def __init__(self, val=" ", left=None, right=None):
92+
# self.val = val
93+
# self.left = left
94+
# self.right = right
95+
class Solution:
96+
def checkEquivalence(self, root1: 'Node', root2: 'Node') -> bool:
97+
def calc(ans, left, right, op):
98+
for i in range(26):
99+
if op == '+':
100+
ans[i] = left[i] + right[i]
101+
else:
102+
ans[i] = left[i] - right[i]
103+
104+
def dfs(root):
105+
ans = [0] * 26
106+
if not root:
107+
return ans
108+
if root.val in ['+', '-']:
109+
left, right = dfs(root.left), dfs(root.right)
110+
calc(ans, left, right, root.val)
111+
else:
112+
ans[ord(root.val) - ord('a')] += 1
113+
return ans
114+
115+
return dfs(root1) == dfs(root2)
68116
```
69117

70118
### **Java**
71119

72120
<!-- 这里可写当前语言的特殊实现逻辑 -->
73121

74122
```java
123+
/**
124+
* Definition for a binary tree node.
125+
* class Node {
126+
* char val;
127+
* Node left;
128+
* Node right;
129+
* Node() {this.val = ' ';}
130+
* Node(char val) { this.val = val; }
131+
* Node(char val, Node left, Node right) {
132+
* this.val = val;
133+
* this.left = left;
134+
* this.right = right;
135+
* }
136+
* }
137+
*/
138+
class Solution {
139+
private int[] counter;
140+
141+
public boolean checkEquivalence(Node root1, Node root2) {
142+
counter = new int[26];
143+
dfs(root1, 1);
144+
dfs(root2, -1);
145+
for (int n : counter) {
146+
if (n != 0) {
147+
return false;
148+
}
149+
}
150+
return true;
151+
}
152+
153+
private void dfs(Node root, int incr) {
154+
if (root == null) {
155+
return;
156+
}
157+
dfs(root.left, incr);
158+
dfs(root.right, incr);
159+
if (root.val != '+') {
160+
counter[root.val - 'a'] += incr;
161+
}
162+
}
163+
}
164+
```
165+
166+
```java
167+
/**
168+
* Definition for a binary tree node.
169+
* class Node {
170+
* char val;
171+
* Node left;
172+
* Node right;
173+
* Node() {this.val = ' ';}
174+
* Node(char val) { this.val = val; }
175+
* Node(char val, Node left, Node right) {
176+
* this.val = val;
177+
* this.left = left;
178+
* this.right = right;
179+
* }
180+
* }
181+
*/
182+
class Solution {
183+
public boolean checkEquivalence(Node root1, Node root2) {
184+
int[] ans1 = dfs(root1);
185+
int[] ans2 = dfs(root2);
186+
for (int i = 0; i < 26; ++i) {
187+
if (ans1[i] != ans2[i]) {
188+
return false;
189+
}
190+
}
191+
return true;
192+
}
193+
194+
private int[] dfs(Node root) {
195+
int[] ans = new int[26];
196+
if (root == null) {
197+
return ans;
198+
}
199+
if (root.val == '+' || root.val == '-') {
200+
int[] left = dfs(root.left);
201+
int[] right = dfs(root.right);
202+
calc(ans, left, right, root.val);
203+
} else {
204+
++ans[root.val - 'a'];
205+
}
206+
return ans;
207+
}
208+
209+
private void calc(int[] ans, int[] left, int[] right, char op) {
210+
for (int i = 0; i < 26; ++i) {
211+
ans[i] = op == '+' ? left[i] + right[i] : left[i] - right[i];
212+
}
213+
}
214+
}
215+
```
216+
217+
### **C++**
218+
219+
```cpp
220+
/**
221+
* Definition for a binary tree node.
222+
* struct Node {
223+
* char val;
224+
* Node *left;
225+
* Node *right;
226+
* Node() : val(' '), left(nullptr), right(nullptr) {}
227+
* Node(char x) : val(x), left(nullptr), right(nullptr) {}
228+
* Node(char x, Node *left, Node *right) : val(x), left(left), right(right) {}
229+
* };
230+
*/
231+
class Solution {
232+
public:
233+
vector<int> counter;
234+
235+
bool checkEquivalence(Node* root1, Node* root2) {
236+
counter.resize(26);
237+
dfs(root1, 1);
238+
dfs(root2, -1);
239+
return count(counter.begin(), counter.end(), 0) == 26;
240+
}
241+
242+
void dfs(Node* root, int incr) {
243+
if (!root) return;
244+
dfs(root->left, incr);
245+
dfs(root->right, incr);
246+
if (root->val != '+') counter[root->val - 'a'] += incr;
247+
}
248+
};
249+
```
75250
251+
```cpp
252+
/**
253+
* Definition for a binary tree node.
254+
* struct Node {
255+
* char val;
256+
* Node *left;
257+
* Node *right;
258+
* Node() : val(' '), left(nullptr), right(nullptr) {}
259+
* Node(char x) : val(x), left(nullptr), right(nullptr) {}
260+
* Node(char x, Node *left, Node *right) : val(x), left(left), right(right) {}
261+
* };
262+
*/
263+
class Solution {
264+
public:
265+
bool checkEquivalence(Node* root1, Node* root2) {
266+
return dfs(root1) == dfs(root2);
267+
}
268+
269+
vector<int> dfs(Node* root) {
270+
vector<int> ans(26);
271+
if (!root) return ans;
272+
if (root->val == '+' || root->val == '-')
273+
{
274+
auto left = dfs(root->left);
275+
auto right = dfs(root->right);
276+
calc(ans, left, right, root->val);
277+
return ans;
278+
}
279+
++ans[root->val - 'a'];
280+
return ans;
281+
}
282+
283+
void calc(vector<int>& ans, vector<int>& left, vector<int>& right, char op) {
284+
for (int i = 0; i < 26; ++i)
285+
ans[i] = op == '+' ? left[i] + right[i] : left[i] - right[i];
286+
}
287+
};
76288
```
77289

78290
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /