Skip to content

Commit 3c5bf49

Browse files
add 1660
1 parent e7781b1 commit 3c5bf49

File tree

2 files changed

+51
-0
lines changed
  • paginated_contents/algorithms/2nd_thousand
  • src/main/java/com/fishercoder/solutions/secondthousand

2 files changed

+51
-0
lines changed

Diff for: paginated_contents/algorithms/2nd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
| 1664 | [Ways to Make a Fair Array](https://leetcode.com/problems/ways-to-make-a-fair-array/) | [Javascript](./javascript/_1664.js) || Medium | Greedy |
148148
| 1663 | [Smallest String With A Given Numeric Value](https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1663.java) | [:tv:](https://youtu.be/o3MRJfsoUrw) | Medium | Greedy |
149149
| 1662 | [Check If Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1662.java) || Easy | String |
150+
| 1660 | [Correct a Binary Tree](https://leetcode.com/problems/correct-a-binary-tree/) | [Javascript](./javascript/_1658.js), [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1660.java) || Medium | BFS, Tree |
150151
| 1658 | [Minimum Operations to Reduce X to Zero](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/) | [Javascript](./javascript/_1658.js), [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1658.java) || Medium | Greedy |
151152
| 1657 | [Determine if Two Strings Are Close](https://leetcode.com/problems/determine-if-two-strings-are-close/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1657.java) | [:tv:](https://youtu.be/-jXQK-UeChU) | Medium | Greedy |
152153
| 1656 | [Design an Ordered Stream](https://leetcode.com/problems/design-an-ordered-stream/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1656.java) || Easy | Array, Design |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.fishercoder.solutions.secondthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.HashSet;
6+
import java.util.LinkedList;
7+
import java.util.Queue;
8+
import java.util.Set;
9+
10+
public class _1660 {
11+
public static class Solution1 {
12+
/**
13+
* First off, this problem description is confusing.
14+
* Second, that aside, I learned a cool technique to pass TreeNode[]{node, nodeParent} into the queue
15+
* so that you can easily reference one node's parent without building an additional hashmap.
16+
* Third, there's no easy way to write unit tests for this problem...
17+
*/
18+
public TreeNode correctBinaryTree(TreeNode root) {
19+
Queue<TreeNode[]> q = new LinkedList<>();
20+
q.offer(new TreeNode[]{root, null});
21+
Set<Integer> visited = new HashSet<>();
22+
visited.add(root.val);
23+
while (!q.isEmpty()) {
24+
int size = q.size();
25+
for (int i = 0; i < size; i++) {
26+
TreeNode[] curr = q.poll();
27+
TreeNode node = curr[0];
28+
TreeNode nodeParent = curr[1];
29+
if (node.right != null && visited.contains(node.right.val)) {
30+
if (nodeParent.left == node) {
31+
nodeParent.left = null;
32+
} else {
33+
nodeParent.right = null;
34+
}
35+
return root;
36+
}
37+
if (node.left != null) {
38+
q.offer(new TreeNode[]{node.left, node});
39+
visited.add(node.left.val);
40+
}
41+
if (node.right != null) {
42+
q.offer(new TreeNode[]{node.right, node});
43+
visited.add(node.right.val);
44+
}
45+
}
46+
}
47+
return root;
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)