Skip to content

Commit 999a423

Browse files
committed
feat: add js solution to lc problem: No.0872
1 parent 9a701ae commit 999a423

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

+18
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

+18
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 -->
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)