Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 34026c7

Browse files
committedMar 28, 2025
Add solution #951
1 parent 09f2871 commit 34026c7

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,036 LeetCode solutions in JavaScript
1+
# 1,037 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -760,6 +760,7 @@
760760
948|[Bag of Tokens](./solutions/0948-bag-of-tokens.js)|Medium|
761761
949|[Largest Time for Given Digits](./solutions/0949-largest-time-for-given-digits.js)|Medium|
762762
950|[Reveal Cards In Increasing Order](./solutions/0950-reveal-cards-in-increasing-order.js)|Medium|
763+
951|[Flip Equivalent Binary Trees](./solutions/0951-flip-equivalent-binary-trees.js)|Medium|
763764
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|
764765
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
765766
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 951. Flip Equivalent Binary Trees
3+
* https://leetcode.com/problems/flip-equivalent-binary-trees/
4+
* Difficulty: Medium
5+
*
6+
* For a binary tree T, we can define a flip operation as follows: choose any node, and swap the
7+
* left and right child subtrees.
8+
*
9+
* A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y
10+
* after some number of flip operations.
11+
*
12+
* Given the roots of two binary trees root1 and root2, return true if the two trees are flip
13+
* equivalent or false otherwise.
14+
*/
15+
16+
/**
17+
* Definition for a binary tree node.
18+
* function TreeNode(val, left, right) {
19+
* this.val = (val===undefined ? 0 : val)
20+
* this.left = (left===undefined ? null : left)
21+
* this.right = (right===undefined ? null : right)
22+
* }
23+
*/
24+
/**
25+
* @param {TreeNode} root1
26+
* @param {TreeNode} root2
27+
* @return {boolean}
28+
*/
29+
var flipEquiv = function(root1, root2) {
30+
return helper(root1, root2);
31+
32+
function helper(node1, node2) {
33+
if (!node1 && !node2) return true;
34+
if (!node1 || !node2 || node1.val !== node2.val) return false;
35+
return (helper(node1.left, node2.left) && helper(node1.right, node2.right))
36+
|| (helper(node1.left, node2.right) && helper(node1.right, node2.left));
37+
}
38+
};

0 commit comments

Comments
 (0)
Please sign in to comment.