diff --git a/solution/0900-0999/0951.Flip Equivalent Binary Trees/README.md b/solution/0900-0999/0951.Flip Equivalent Binary Trees/README.md index c86a9c60d77d3..de294a24867bc 100644 --- a/solution/0900-0999/0951.Flip Equivalent Binary Trees/README.md +++ b/solution/0900-0999/0951.Flip Equivalent Binary Trees/README.md @@ -182,6 +182,34 @@ func flipEquiv(root1 *TreeNode, root2 *TreeNode) bool { } ``` +#### TypeScript + +```ts +function flipEquiv(root1: TreeNode | null, root2: TreeNode | null): boolean { + if (root1 === root2) return true; + if (!root1 || !root2 || root1?.val !== root2?.val) return false; + + const { left: l1, right: r1 } = root1!; + const { left: l2, right: r2 } = root2!; + + return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2)); +} +``` + +#### JavaScript + +```js +function flipEquiv(root1, root2) { + if (root1 === root2) return true; + if (!root1 || !root2 || root1?.val !== root2?.val) return false; + + const { left: l1, right: r1 } = root1; + const { left: l2, right: r2 } = root2; + + return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2)); +} +``` + diff --git a/solution/0900-0999/0951.Flip Equivalent Binary Trees/README_EN.md b/solution/0900-0999/0951.Flip Equivalent Binary Trees/README_EN.md index dcc51a67c5cc1..4940fe3be8258 100644 --- a/solution/0900-0999/0951.Flip Equivalent Binary Trees/README_EN.md +++ b/solution/0900-0999/0951.Flip Equivalent Binary Trees/README_EN.md @@ -178,6 +178,34 @@ func flipEquiv(root1 *TreeNode, root2 *TreeNode) bool { } ``` +#### TypeScript + +```ts +function flipEquiv(root1: TreeNode | null, root2: TreeNode | null): boolean { + if (root1 === root2) return true; + if (!root1 || !root2 || root1?.val !== root2?.val) return false; + + const { left: l1, right: r1 } = root1!; + const { left: l2, right: r2 } = root2!; + + return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2)); +} +``` + +#### JavaScript + +```js +function flipEquiv(root1, root2) { + if (root1 === root2) return true; + if (!root1 || !root2 || root1?.val !== root2?.val) return false; + + const { left: l1, right: r1 } = root1; + const { left: l2, right: r2 } = root2; + + return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2)); +} +``` + diff --git a/solution/0900-0999/0951.Flip Equivalent Binary Trees/Solution.js b/solution/0900-0999/0951.Flip Equivalent Binary Trees/Solution.js new file mode 100644 index 0000000000000..60af04dec4ae8 --- /dev/null +++ b/solution/0900-0999/0951.Flip Equivalent Binary Trees/Solution.js @@ -0,0 +1,9 @@ +function flipEquiv(root1, root2) { + if (root1 === root2) return true; + if (!root1 || !root2 || root1?.val !== root2?.val) return false; + + const { left: l1, right: r1 } = root1; + const { left: l2, right: r2 } = root2; + + return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2)); +} diff --git a/solution/0900-0999/0951.Flip Equivalent Binary Trees/Solution.ts b/solution/0900-0999/0951.Flip Equivalent Binary Trees/Solution.ts new file mode 100644 index 0000000000000..a3a67fb2335fe --- /dev/null +++ b/solution/0900-0999/0951.Flip Equivalent Binary Trees/Solution.ts @@ -0,0 +1,9 @@ +function flipEquiv(root1: TreeNode | null, root2: TreeNode | null): boolean { + if (root1 === root2) return true; + if (!root1 || !root2 || root1?.val !== root2?.val) return false; + + const { left: l1, right: r1 } = root1!; + const { left: l2, right: r2 } = root2!; + + return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2)); +}