File tree 3 files changed +51
-0
lines changed
solution/0800-0899/0872.Leaf-Similar Trees
3 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -224,6 +224,24 @@ impl Solution {
224
224
}
225
225
```
226
226
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
+
227
245
<!-- tabs:end -->
228
246
229
247
<!-- end -->
Original file line number Diff line number Diff line change @@ -214,6 +214,24 @@ impl Solution {
214
214
}
215
215
```
216
216
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
+
217
235
<!-- tabs:end -->
218
236
219
237
<!-- end -->
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments