Skip to content

Commit 678cde1

Browse files
havanagrawalfishercoder1534
authored andcommitted
Add solution for 951 (#49)
1 parent a701364 commit 678cde1

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Your ideas/fixes/algorithms are more than welcome!
5050
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_965.java) | O(n) | O(h) | |Easy| DFS, recursion|
5151
|961|[N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_961.java) | O(n) | O(1) | |Easy|
5252
|953|[Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_953.java) | O(1) | O(1) | |Easy|
53+
|951|[Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_951.java) | O(n) | O(h) | |Medium| Tree, DFS, recursion|
5354
|950|[Reveal Cards In Increasing Order](https://leetcode.com/problems/reveal-cards-in-increasing-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_950.java) | O(nlogn) | O(n) | |Medium|
5455
|944|[Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_944.java) | O(n) | O(1) | |Easy|
5556
|942|[DI String Match](https://leetcode.com/problems/di-string-match/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_942.java) | O(n) | O(n) | |Easy|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
/**
6+
* 951. Flip Equivalent Binary Trees
7+
*
8+
* For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees.
9+
*
10+
* A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations.
11+
*
12+
* Write a function that determines whether two binary trees are flip equivalent. The trees are given by root nodes root1 and root2.
13+
*
14+
* Note:
15+
* * Each tree will have at most 100 nodes.
16+
* * Each value in each tree will be a unique integer in the range [0, 99].
17+
*/
18+
19+
public class _951 {
20+
public static class Solution1 {
21+
public boolean flipEquiv(TreeNode root1, TreeNode root2) {
22+
if (root1 == null && root2 == null) return true;
23+
if (root1 == null || root2 == null) return false;
24+
25+
if (root1.val != root2.val) return false;
26+
27+
return (
28+
(flipEquiv(root1.left, root2.left) && flipEquiv(root1.right, root2.right)) ||
29+
(flipEquiv(root1.left, root2.right) && flipEquiv(root1.right, root2.left))
30+
);
31+
}
32+
}
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._951;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.Assert.assertFalse;
12+
import static org.junit.Assert.assertTrue;
13+
14+
public class _951Test {
15+
private static _951.Solution1 solution1;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _951.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
TreeNode root1 = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, null, null, null, 7, 8));
25+
TreeNode root2 = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2, null, 6, 4, 5, null, null, null, null, 8, 7));
26+
assertTrue(solution1.flipEquiv(root1, root2));
27+
}
28+
29+
@Test
30+
public void test2() {
31+
TreeNode root1 = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3));
32+
TreeNode root2 = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 4));
33+
assertFalse(solution1.flipEquiv(root1, root2));
34+
}
35+
}

0 commit comments

Comments
 (0)