Skip to content

Commit 1309578

Browse files
authored
feat: add js solution to lc problem: No.0872 (doocs#2621)
1 parent a693e1d commit 1309578

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

solution/0800-0899/0872.Leaf-Similar Trees/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,24 @@ impl Solution {
224224
}
225225
```
226226

227+
```js
228+
var leafSimilar = function (root1, root2) {
229+
const dfs = root => {
230+
if (!root) {
231+
return [];
232+
}
233+
let ans = [...dfs(root.left), ...dfs(root.right)];
234+
if (!ans.length) {
235+
ans = [root.val];
236+
}
237+
return ans;
238+
};
239+
const l1 = dfs(root1);
240+
const l2 = dfs(root2);
241+
return l1.toString() === l2.toString();
242+
};
243+
```
244+
227245
<!-- tabs:end -->
228246

229247
<!-- end -->

solution/0800-0899/0872.Leaf-Similar Trees/README_EN.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,24 @@ impl Solution {
214214
}
215215
```
216216

217+
```js
218+
var leafSimilar = function (root1, root2) {
219+
const dfs = root => {
220+
if (!root) {
221+
return [];
222+
}
223+
let ans = [...dfs(root.left), ...dfs(root.right)];
224+
if (!ans.length) {
225+
ans = [root.val];
226+
}
227+
return ans;
228+
};
229+
const l1 = dfs(root1);
230+
const l2 = dfs(root2);
231+
return l1.toString() === l2.toString();
232+
};
233+
```
234+
217235
<!-- tabs:end -->
218236

219237
<!-- end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var leafSimilar = function (root1, root2) {
2+
const dfs = root => {
3+
if (!root) {
4+
return [];
5+
}
6+
let ans = [...dfs(root.left), ...dfs(root.right)];
7+
if (!ans.length) {
8+
ans = [root.val];
9+
}
10+
return ans;
11+
};
12+
const l1 = dfs(root1);
13+
const l2 = dfs(root2);
14+
return l1.toString() === l2.toString();
15+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy