Skip to content

Commit 809eb84

Browse files
committed
add 951 java, progress
1 parent c91afbe commit 809eb84

File tree

6 files changed

+268
-8
lines changed

6 files changed

+268
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@
505505
897| [Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/) | [Python](./leetcode_python/Tree/increasing-order-search-tree.py) | _O(n)_ | _O(h)_ | Easy | `DFS`, `good basic`,`inorder` | AGAIN** (2)
506506
919| [Complete Binary Tree Inserter](https://leetcode.com/problems/complete-binary-tree-inserter/) | [Python](./leetcode_python/Tree/complete-binary-tree-inserter.py) | ctor: _O(n)_ <br> insert: _O(1)_ <br> get_root: _O(1)_ | _O(n)_ | Medium |`bst`| AGAIN** (2)
507507
938| [Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/) | [Python](./leetcode_python/Tree/range-sum-of-bst.py) | _O(n)_ | _O(h)_ | Medium | DFS, check `# 108 Convert Sorted Array to Binary Search Tree`| AGAIN* (2)
508-
951| [Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees/) | [Python](./leetcode_python/Tree/flip-equivalent-binary-trees.py) | _O(n)_ | _O(h)_ | Medium | DFS | AGAIN* (2)
508+
951| [Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees/) | [Python](./leetcode_python/Tree/flip-equivalent-binary-trees.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Tree/FlipEquivalentBinaryTrees.java)| _O(n)_ | _O(h)_ | Medium | recursion, tree, DFS, google | AGAIN***s (3)
509509
958| [Check Completeness of a Binary Tree](https://leetcode.com/problems/check-completeness-of-a-binary-tree/) | [Python](./leetcode_python/Tree/check-completeness-of-a-binary-tree.py) | _O(n)_ | _O(w)_ | Medium | `BFS`, `DFS`, `basic` | AGAIN** (2)
510510
965| [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [Python](./leetcode_python/Tree/univalued-binary-tree.py) | _O(n)_ | _O(h)_ | Easy | `DFS`, `BFS`, `good basic`| AGAIN** (2)
511511
971| [Flip Binary Tree To Match Preorder Traversal](https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/) | [Python](./leetcode_python/Tree/flip-binary-tree-to-match-preorder-traversal.py) | _O(n)_ | _O(h)_ | Medium | DFS, `trick`| AGAIN** (2) (not start)

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-10-24
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-10-20
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241024: 951
12
20241020: 163,1048
23
20241019: 298,729
34
20241018: 1146

data/to_review.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2024-12-18 -> ['951']
12
2024-12-14 -> ['163,1048']
23
2024-12-13 -> ['298,729']
34
2024-12-12 -> ['1146']
@@ -7,7 +8,7 @@
78
2024-12-05 -> ['528,334']
89
2024-12-03 -> ['1145']
910
2024-11-30 -> ['1145,1219']
10-
2024-11-27 -> ['524,221,889']
11+
2024-11-27 -> ['951', '524,221,889']
1112
2024-11-26 -> ['743,889']
1213
2024-11-25 -> ['837']
1314
2024-11-23 -> ['163,1048', '981']
@@ -18,24 +19,25 @@
1819
2024-11-17 -> ['737', '363']
1920
2024-11-16 -> ['686,734,737', '1032,844,1011']
2021
2024-11-15 -> ['353', '947']
21-
2024-11-14 -> ['528,334']
22+
2024-11-14 -> ['951', '528,334']
2223
2024-11-12 -> ['1145', '753']
2324
2024-11-11 -> ['727']
2425
2024-11-10 -> ['163,1048']
2526
2024-11-09 -> ['298,729', '1145,1219']
2627
2024-11-08 -> ['1146']
27-
2024-11-06 -> ['524,221,889']
28+
2024-11-06 -> ['951', '524,221,889']
2829
2024-11-05 -> ['743,889']
2930
2024-11-04 -> ['737', '837', '659']
3031
2024-11-03 -> ['686,734,737', '801,552']
3132
2024-11-02 -> ['163,1048', '353', '981', '1057,1066,1110']
32-
2024-11-01 -> ['298,729', '528,334', '1087']
33+
2024-11-01 -> ['951', '298,729', '528,334', '1087']
3334
2024-10-31 -> ['1146']
3435
2024-10-30 -> ['1145', '939']
36+
2024-10-29 -> ['951']
3537
2024-10-28 -> ['163,1048', '430']
36-
2024-10-27 -> ['298,729', '737', '1145,1219', '363']
37-
2024-10-26 -> ['1146', '686,734,737', '1032,844,1011']
38-
2024-10-25 -> ['163,1048', '353', '947', '1110, 1055']
38+
2024-10-27 -> ['951', '298,729', '737', '1145,1219', '363']
39+
2024-10-26 -> ['951', '1146', '686,734,737', '1032,844,1011']
40+
2024-10-25 -> ['951', '163,1048', '353', '947', '1110, 1055']
3941
2024-10-24 -> ['298,729', '528,334', '524,221,889']
4042
2024-10-23 -> ['163,1048', '1146', '743,889']
4143
2024-10-22 -> ['163,1048', '298,729', '737', '1145', '837', '753']
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,48 @@ public int longestStrChain(String[] words) {
19121912
return 0;
19131913
}
19141914

1915+
// LC 951
1916+
// https://leetcode.com/problems/flip-equivalent-binary-trees/
1917+
// 7.23 pm - 7.40 pm
1918+
public boolean flipEquiv(TreeNode root1, TreeNode root2) {
1919+
1920+
// edge cases
1921+
if (root1 == null || root2 == null) {
1922+
return false;
1923+
}
1924+
1925+
if (root1.val != root2.val) {
1926+
return false;
1927+
}
1928+
1929+
if (root1 == null && root2 == null) {
1930+
return true;
1931+
}
1932+
1933+
1934+
return isSameTree(root1, root2);
1935+
}
1936+
1937+
private boolean isSameTree(TreeNode root1, TreeNode root2) {
1938+
1939+
if (root1 == null || root2 == null) {
1940+
return false;
1941+
}
1942+
1943+
if (root1.val != root2.val) {
1944+
return false;
1945+
}
1946+
1947+
if (root1 == null && root2 == null) {
1948+
return true;
1949+
}
1950+
1951+
return (isSameTree(root1.left, root2.right)
1952+
&& isSameTree(root1.right, root2.left))
1953+
|| (isSameTree(root1.right, root2.left)
1954+
&& isSameTree(root1.right, root2.left)) ;
1955+
//&& isSameTree(root1, root2); // ?? needed ?
1956+
}
19151957

19161958
}
19171959

0 commit comments

Comments
 (0)