diff --git a/README.md b/README.md index 3dac1a1d3e..05b5726101 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ Your ideas/fixes/algorithms are more than welcome! |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| |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| |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| +|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| |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| |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| |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| diff --git a/src/main/java/com/fishercoder/solutions/_951.java b/src/main/java/com/fishercoder/solutions/_951.java new file mode 100644 index 0000000000..20ea0da130 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/_951.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions; + +import com.fishercoder.common.classes.TreeNode; + +/** + * 951. Flip Equivalent Binary Trees + * + * For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. + * + * 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. + * + * Write a function that determines whether two binary trees are flip equivalent. The trees are given by root nodes root1 and root2. + * + * Note: + * * Each tree will have at most 100 nodes. + * * Each value in each tree will be a unique integer in the range [0, 99]. + */ + +public class _951 { + public static class Solution1 { + public boolean flipEquiv(TreeNode root1, TreeNode root2) { + if (root1 == null && root2 == null) return true; + if (root1 == null || root2 == null) return false; + + if (root1.val != root2.val) return false; + + return ( + (flipEquiv(root1.left, root2.left) && flipEquiv(root1.right, root2.right)) || + (flipEquiv(root1.left, root2.right) && flipEquiv(root1.right, root2.left)) + ); + } + } +} diff --git a/src/test/java/com/fishercoder/_951Test.java b/src/test/java/com/fishercoder/_951Test.java new file mode 100644 index 0000000000..4750a22c18 --- /dev/null +++ b/src/test/java/com/fishercoder/_951Test.java @@ -0,0 +1,35 @@ +package com.fishercoder; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions._951; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class _951Test { + private static _951.Solution1 solution1; + + @BeforeClass + public static void setup() { + solution1 = new _951.Solution1(); + } + + @Test + public void test1() { + TreeNode root1 = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, null, null, null, 7, 8)); + TreeNode root2 = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2, null, 6, 4, 5, null, null, null, null, 8, 7)); + assertTrue(solution1.flipEquiv(root1, root2)); + } + + @Test + public void test2() { + TreeNode root1 = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); + TreeNode root2 = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 4)); + assertFalse(solution1.flipEquiv(root1, root2)); + } +}
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: