Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add js solution to lc problem: No.0872 #2621

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add js solution to lc problem: No.0872
  • Loading branch information
thinkasany committed Apr 19, 2024
commit 999a4231d84ba17bc59b03bda219a6b5bb70d90e
18 changes: 18 additions & 0 deletions solution/0800-0899/0872.Leaf-Similar Trees/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,24 @@ impl Solution {
}
```

```js
var leafSimilar = function (root1, root2) {
const dfs = root => {
if (!root) {
return [];
}
let ans = [...dfs(root.left), ...dfs(root.right)];
if (!ans.length) {
ans = [root.val];
}
return ans;
};
const l1 = dfs(root1);
const l2 = dfs(root2);
return l1.toString() === l2.toString();
};
```

<!-- tabs:end -->

<!-- end -->
18 changes: 18 additions & 0 deletions solution/0800-0899/0872.Leaf-Similar Trees/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,24 @@ impl Solution {
}
```

```js
var leafSimilar = function (root1, root2) {
const dfs = root => {
if (!root) {
return [];
}
let ans = [...dfs(root.left), ...dfs(root.right)];
if (!ans.length) {
ans = [root.val];
}
return ans;
};
const l1 = dfs(root1);
const l2 = dfs(root2);
return l1.toString() === l2.toString();
};
```

<!-- tabs:end -->

<!-- end -->
15 changes: 15 additions & 0 deletions solution/0800-0899/0872.Leaf-Similar Trees/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var leafSimilar = function (root1, root2) {
const dfs = root => {
if (!root) {
return [];
}
let ans = [...dfs(root.left), ...dfs(root.right)];
if (!ans.length) {
ans = [root.val];
}
return ans;
};
const l1 = dfs(root1);
const l2 = dfs(root2);
return l1.toString() === l2.toString();
};
Loading