Skip to content

Commit e9ba978

Browse files
authored
feat: add solutions to lc problem: No.0951 (doocs#3667)
1 parent 9850a67 commit e9ba978

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

solution/0900-0999/0951.Flip Equivalent Binary Trees/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,34 @@ func flipEquiv(root1 *TreeNode, root2 *TreeNode) bool {
182182
}
183183
```
184184

185+
#### TypeScript
186+
187+
```ts
188+
function flipEquiv(root1: TreeNode | null, root2: TreeNode | null): boolean {
189+
if (root1 === root2) return true;
190+
if (!root1 || !root2 || root1?.val !== root2?.val) return false;
191+
192+
const { left: l1, right: r1 } = root1!;
193+
const { left: l2, right: r2 } = root2!;
194+
195+
return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2));
196+
}
197+
```
198+
199+
#### JavaScript
200+
201+
```js
202+
function flipEquiv(root1, root2) {
203+
if (root1 === root2) return true;
204+
if (!root1 || !root2 || root1?.val !== root2?.val) return false;
205+
206+
const { left: l1, right: r1 } = root1;
207+
const { left: l2, right: r2 } = root2;
208+
209+
return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2));
210+
}
211+
```
212+
185213
<!-- tabs:end -->
186214
187215
<!-- solution:end -->

solution/0900-0999/0951.Flip Equivalent Binary Trees/README_EN.md

+28
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,34 @@ func flipEquiv(root1 *TreeNode, root2 *TreeNode) bool {
178178
}
179179
```
180180

181+
#### TypeScript
182+
183+
```ts
184+
function flipEquiv(root1: TreeNode | null, root2: TreeNode | null): boolean {
185+
if (root1 === root2) return true;
186+
if (!root1 || !root2 || root1?.val !== root2?.val) return false;
187+
188+
const { left: l1, right: r1 } = root1!;
189+
const { left: l2, right: r2 } = root2!;
190+
191+
return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2));
192+
}
193+
```
194+
195+
#### JavaScript
196+
197+
```js
198+
function flipEquiv(root1, root2) {
199+
if (root1 === root2) return true;
200+
if (!root1 || !root2 || root1?.val !== root2?.val) return false;
201+
202+
const { left: l1, right: r1 } = root1;
203+
const { left: l2, right: r2 } = root2;
204+
205+
return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2));
206+
}
207+
```
208+
181209
<!-- tabs:end -->
182210
183211
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function flipEquiv(root1, root2) {
2+
if (root1 === root2) return true;
3+
if (!root1 || !root2 || root1?.val !== root2?.val) return false;
4+
5+
const { left: l1, right: r1 } = root1;
6+
const { left: l2, right: r2 } = root2;
7+
8+
return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2));
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function flipEquiv(root1: TreeNode | null, root2: TreeNode | null): boolean {
2+
if (root1 === root2) return true;
3+
if (!root1 || !root2 || root1?.val !== root2?.val) return false;
4+
5+
const { left: l1, right: r1 } = root1!;
6+
const { left: l2, right: r2 } = root2!;
7+
8+
return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2));
9+
}

0 commit comments

Comments
 (0)