|
| 1 | +package LeetCodeJava.Tree; |
| 2 | + |
| 3 | +// https://leetcode.com/problems/flip-equivalent-binary-trees/description/ |
| 4 | + |
| 5 | +import LeetCodeJava.DataStructure.TreeNode; |
| 6 | + |
| 7 | +import java.util.Stack; |
| 8 | + |
| 9 | +/** |
| 10 | + * 951. Flip Equivalent Binary Trees |
| 11 | + * Medium |
| 12 | + * Topics |
| 13 | + * Companies |
| 14 | + * For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. |
| 15 | + * |
| 16 | + * 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. |
| 17 | + * |
| 18 | + * Given the roots of two binary trees root1 and root2, return true if the two trees are flip equivalent or false otherwise. |
| 19 | + * |
| 20 | + * |
| 21 | + * |
| 22 | + * Example 1: |
| 23 | + * |
| 24 | + * Flipped Trees Diagram |
| 25 | + * Input: root1 = [1,2,3,4,5,6,null,null,null,7,8], root2 = [1,3,2,null,6,4,5,null,null,null,null,8,7] |
| 26 | + * Output: true |
| 27 | + * Explanation: We flipped at nodes with values 1, 3, and 5. |
| 28 | + * Example 2: |
| 29 | + * |
| 30 | + * Input: root1 = [], root2 = [] |
| 31 | + * Output: true |
| 32 | + * Example 3: |
| 33 | + * |
| 34 | + * Input: root1 = [], root2 = [1] |
| 35 | + * Output: false |
| 36 | + * |
| 37 | + * |
| 38 | + * Constraints: |
| 39 | + * |
| 40 | + * The number of nodes in each tree is in the range [0, 100]. |
| 41 | + * Each tree will have unique node values in the range [0, 99]. |
| 42 | + * |
| 43 | + */ |
| 44 | +public class FlipEquivalentBinaryTrees { |
| 45 | + |
| 46 | + /** |
| 47 | + * Definition for a binary tree node. |
| 48 | + * public class TreeNode { |
| 49 | + * int val; |
| 50 | + * TreeNode left; |
| 51 | + * TreeNode right; |
| 52 | + * TreeNode() {} |
| 53 | + * TreeNode(int val) { this.val = val; } |
| 54 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 55 | + * this.val = val; |
| 56 | + * this.left = left; |
| 57 | + * this.right = right; |
| 58 | + * } |
| 59 | + * } |
| 60 | + */ |
| 61 | + // V0 |
| 62 | + // TODO : implement |
| 63 | +// public boolean flipEquiv(TreeNode root1, TreeNode root2) { |
| 64 | +// |
| 65 | +// } |
| 66 | + |
| 67 | + // V1 |
| 68 | + // IDEA : DFS (fixed by gpt) |
| 69 | + public boolean flipEquiv_1(TreeNode root1, TreeNode root2) { |
| 70 | + // Both trees are null, they are equivalent |
| 71 | + if (root1 == null && root2 == null) { |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + // If one is null and the other is not, or the values don't match |
| 76 | + if (root1 == null || root2 == null || root1.val != root2.val) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + // Check both configurations: |
| 81 | + // 1. No flip: compare root1.left with root2.left and root1.right with root2.right |
| 82 | + // 2. Flip: compare root1.left with root2.right and root1.right with root2.left |
| 83 | + return (flipEquiv_1(root1.left, root2.left) && flipEquiv_1(root1.right, root2.right)) || |
| 84 | + (flipEquiv_1(root1.left, root2.right) && flipEquiv_1(root1.right, root2.left)); |
| 85 | + } |
| 86 | + |
| 87 | + // V2-1 |
| 88 | + // IDEA : Recursion (Top-down Traversal) |
| 89 | + // https://leetcode.com/problems/flip-equivalent-binary-trees/editorial/ |
| 90 | + public boolean flipEquiv_2_1(TreeNode root1, TreeNode root2) { |
| 91 | + // Both trees are empty |
| 92 | + if (root1 == null && root2 == null) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + // Just one of the trees is empty |
| 96 | + if (root1 == null || root2 == null) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + // Corresponding values differ |
| 100 | + if (root1.val != root2.val) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + // Check if corresponding subtrees are flip equivalent |
| 105 | + boolean noSwap = |
| 106 | + flipEquiv_2_1(root1.left, root2.left) && |
| 107 | + flipEquiv_2_1(root1.right, root2.right); |
| 108 | + // Check if opposite subtrees are flip equivalent |
| 109 | + boolean swap = |
| 110 | + flipEquiv_2_1(root1.left, root2.right) && |
| 111 | + flipEquiv_2_1(root1.right, root2.left); |
| 112 | + |
| 113 | + return noSwap || swap; |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + // V2-2 |
| 118 | + // IDEA : Iterative DFS (using a Stack) |
| 119 | + // https://leetcode.com/problems/flip-equivalent-binary-trees/editorial/ |
| 120 | + |
| 121 | + // Checks whether the given pair of nodes should be examined - |
| 122 | + // be pushed into the stack |
| 123 | + public boolean checkNodeValues(TreeNode node1, TreeNode node2) { |
| 124 | + if (node1 == null && node2 == null) return true; |
| 125 | + if ( |
| 126 | + node1 != null && node2 != null && node1.val == node2.val |
| 127 | + ) return true; |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + public boolean flipEquiv_2_2(TreeNode root1, TreeNode root2) { |
| 132 | + // Initialize stack to store pairs of nodes |
| 133 | + Stack<TreeNode[]> nodePairStack = new Stack<>(); |
| 134 | + nodePairStack.push(new TreeNode[] { root1, root2 }); |
| 135 | + |
| 136 | + // While the stack is not empty: |
| 137 | + while (!nodePairStack.isEmpty()) { |
| 138 | + TreeNode[] current = nodePairStack.pop(); |
| 139 | + TreeNode node1 = current[0]; |
| 140 | + TreeNode node2 = current[1]; |
| 141 | + |
| 142 | + if (node1 == null && node2 == null) continue; |
| 143 | + if (node1 == null || node2 == null) return false; |
| 144 | + if (node1.val != node2.val) return false; |
| 145 | + |
| 146 | + // Check both configurations: no swap and swap |
| 147 | + if ( |
| 148 | + checkNodeValues(node1.left, node2.left) && |
| 149 | + checkNodeValues(node1.right, node2.right) |
| 150 | + ) { |
| 151 | + nodePairStack.push(new TreeNode[] { node1.left, node2.left }); |
| 152 | + nodePairStack.push(new TreeNode[] { node1.right, node2.right }); |
| 153 | + } else if ( |
| 154 | + checkNodeValues(node1.left, node2.right) && |
| 155 | + checkNodeValues(node1.right, node2.left) |
| 156 | + ) { |
| 157 | + nodePairStack.push(new TreeNode[] { node1.left, node2.right }); |
| 158 | + nodePairStack.push(new TreeNode[] { node1.right, node2.left }); |
| 159 | + } else { |
| 160 | + return false; |
| 161 | + } |
| 162 | + } |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + // V2-3 |
| 168 | + // IDEA : Canonical Forms |
| 169 | + // https://leetcode.com/problems/flip-equivalent-binary-trees/editorial/ |
| 170 | + public void findCanonicalForm(TreeNode root) { |
| 171 | + if (root == null) return; |
| 172 | + |
| 173 | + // Post-order traversal: first bring subtrees into their canonical form |
| 174 | + findCanonicalForm(root.left); |
| 175 | + findCanonicalForm(root.right); |
| 176 | + |
| 177 | + if (root.right == null) return; |
| 178 | + |
| 179 | + // Swap subtrees, so that left is non-empty |
| 180 | + if (root.left == null) { |
| 181 | + root.left = root.right; |
| 182 | + root.right = null; |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + TreeNode left = root.left; |
| 187 | + TreeNode right = root.right; |
| 188 | + // Swap subtrees |
| 189 | + if (left.val > right.val) { |
| 190 | + root.left = right; |
| 191 | + root.right = left; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + public boolean areEquivalent(TreeNode root1, TreeNode root2) { |
| 196 | + if (root1 == null && root2 == null) return true; |
| 197 | + if (root1 == null || root2 == null) return false; |
| 198 | + if (root1.val != root2.val) return false; |
| 199 | + |
| 200 | + return ( |
| 201 | + areEquivalent(root1.left, root2.left) && |
| 202 | + areEquivalent(root1.right, root2.right) |
| 203 | + ); |
| 204 | + } |
| 205 | + |
| 206 | + public boolean flipEquiv_2_3(TreeNode root1, TreeNode root2) { |
| 207 | + findCanonicalForm(root1); |
| 208 | + findCanonicalForm(root2); |
| 209 | + return areEquivalent(root1, root2); |
| 210 | + } |
| 211 | + |
| 212 | +} |
0 commit comments