Skip to content
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
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();
};