Skip to content

Commit 1d2d88b

Browse files
committed
add 117 java
1 parent e0670ee commit 1d2d88b

File tree

2 files changed

+157
-2
lines changed

2 files changed

+157
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@
834834
111| [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)|[Python](./leetcode_python/Recursion/minimum-depth-of-binary-tree.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Recursion/MinimumDepthOfBinaryTree.java)| _O(n)_ | _O(h)_ | Easy |`good basic`,`dfs`,`BST`,`amazon`,`fb`, LC 104| AGAIN******** (5)
835835
114| [Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/)| [Python](./leetcode_python/Recursion/flatten-binary-tree-to-linked-list.py)| _O(n)_ | _O(h)_ | Medium |`BST`, `dfs`, `M$`, `fb`| AGAIN**** (4)
836836
116| [Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)| [Python](./leetcode_python/Recursion/populating-next-right-pointers-in-each-node.py) | _O(n)_ | _O(1)_ | Medium |bfs, dfs, tree, recursion ,AGAIN,`fb`, amazon| AGAIN****** (5)
837-
117| [Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/) |[Python](./leetcode_python/Recursion/populating-next-right-pointers-in-each-node-ii.py) | _O(n)_ | _O(h)_ | Medium | `Populating Next Right Pointers in Each Node I`, bfs, linked list, `amazon`, `fb`| AGAIN****** (3)
837+
117| [Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/) |[Python](./leetcode_python/Recursion/populating-next-right-pointers-in-each-node-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Recursion/PopulatingNextRightPointersInEachNode2.java)| _O(n)_ | _O(h)_ | Medium | `Populating Next Right Pointers in Each Node I`, bfs, linked list, `amazon`, `fb`, google| AGAIN****** (3)
838838
129| [Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/) |[Python](./leetcode_python/Recursion/sum-root-to-leaf-numbers.py) | _O(n)_ | _O(h)_ | Medium |`trick`,`BST`, `dfs`, `fb`| AGAIN*** (4)
839839
156| [Binary Tree Upside Down](https://leetcode.com/problems/binary-tree-upside-down/) | [Python](./leetcode_python/Recursion/binary-tree-upside-down.py) | _O(n)_ | _O(1)_ | Medium |🔒| AGAIN (not start)
840840
241| [Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/) | [Python](./leetcode_python/Recursion/different-ways-to-add-parentheses.py) | _O(n * 4^n / n^(3/2))_ | _O(n * 4^n / n^(3/2))_ | Medium || AGAIN (not start)
@@ -931,7 +931,7 @@
931931
666| [Path Sum IV](https://leetcode.com/problems/path-sum-iv/)| [Python](./leetcode_python/Breadth-First-Search/path-sum-iv.py) | _O(n)_ | _O(w)_ | Medium |🔒 Topological Sort | AGAIN* (3) (not start)
932932
742|[Closest Leaf in a Binary Tree](https://leetcode.com/problems/closest-leaf-in-a-binary-tree/)| [Python](./leetcode_python/Breadth-First-Search/closest-leaf-in-a-binary-tree.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/ClosestLeafInaBinaryTree.java) | _O(n)_ | _O(n)_ | Medium | AGAIN, Graph, bfs+dfs, search, `good trick`, `amazon`| AGAIN********** (6)
933933
743|[Network Delay Time](https://leetcode.com/problems/network-delay-time/)| [Python](./leetcode_python/Breadth-First-Search/network-delay-time.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/NetworkDelayTime.java) | _O(n)_ | _O(n)_ | Medium |Dijlstra, `google`| AGAIN **** (2)
934-
752|[Open the Lock](https://leetcode.com/problems/open-the-lock/)| [Python](./leetcode_python/Breadth-First-Search/open-the-lock.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/OpenTheLock.java)s | _O(k * n^k + d)_ | _O(k * n^k + d)_ | Medium |LC 863, google | AGAIN (not start)
934+
752|[Open the Lock](https://leetcode.com/problems/open-the-lock/)| [Python](./leetcode_python/Breadth-First-Search/open-the-lock.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/OpenTheLock.java) | _O(k * n^k + d)_ | _O(k * n^k + d)_ | Medium |LC 863, google | AGAIN (not start)
935935
787|[Cheapest Flights Within K Stops](https://leetcode.com/problems/cheapest-flights-within-k-stops/)| [Python](./leetcode_python/Breadth-First-Search/cheapest-flights-within-k-stops.py) | _O(\|E\| * log\|V\|)_ | _O(\|E\|)_ | Medium | `Dijkstra's algorithm`, dfs, bfs, graph, priority queue,`amazon`, apple, google, airbnb | AGAIN****** (3) (not start)
936936
815|[Bus Routes](https://leetcode.com/problems/bus-routes/)| [Python](./leetcode_python/Breadth-First-Search/bus-routes.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/BusRoutes.java) | _O(\|E\| * log\|V\|)_ | _O(\|E\|)_ | Hard | shortest route, graph, bfs,`amazon`, google| AGAIN**** (3)
937937
886|[Possible Bipartition](https://leetcode.com/problems/possible-bipartition/)| [Python](./leetcode_python/Breadth-First-Search/possible-bipartition.py) | _O(\|V\| + \|E\|)_ | _O(\|V\| + \|E\|)_| Medium |check `# 785 Is Graph Bipartite?`,`graph`, `AGAIN`,`union find` ,`fb` | AGAIN********** (6)
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package LeetCodeJava.Recursion;
2+
3+
// https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/description/
4+
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
8+
/**
9+
*
10+
Code
11+
Testcase
12+
Test Result
13+
Test Result
14+
117. Populating Next Right Pointers in Each Node II
15+
Solved
16+
Medium
17+
Topics
18+
Companies
19+
Given a binary tree
20+
21+
struct Node {
22+
int val;
23+
Node *left;
24+
Node *right;
25+
Node *next;
26+
}
27+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
28+
29+
Initially, all next pointers are set to NULL.
30+
31+
32+
33+
Example 1:
34+
35+
36+
Input: root = [1,2,3,4,5,null,7]
37+
Output: [1,#,2,3,#,4,5,7,#]
38+
Explanation: Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
39+
Example 2:
40+
41+
Input: root = []
42+
Output: []
43+
44+
45+
Constraints:
46+
47+
The number of nodes in the tree is in the range [0, 6000].
48+
-100 <= Node.val <= 100
49+
50+
51+
Follow-up:
52+
53+
You may only use constant extra space.
54+
The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.
55+
56+
*
57+
*
58+
*/
59+
public class PopulatingNextRightPointersInEachNode2 {
60+
61+
/*
62+
// Definition for a Node.
63+
class Node {
64+
public int val;
65+
public Node left;
66+
public Node right;
67+
public Node next;
68+
69+
public Node() {}
70+
71+
public Node(int _val) {
72+
val = _val;
73+
}
74+
75+
public Node(int _val, Node _left, Node _right, Node _next) {
76+
val = _val;
77+
left = _left;
78+
right = _right;
79+
next = _next;
80+
}
81+
};
82+
*/
83+
84+
// TODO: move to dataStructure package
85+
class Node {
86+
public int val;
87+
public Node left;
88+
public Node right;
89+
public Node next;
90+
91+
public Node() {}
92+
93+
public Node(int _val) {
94+
val = _val;
95+
}
96+
97+
public Node(int _val, Node _left, Node _right, Node _next) {
98+
val = _val;
99+
left = _left;
100+
right = _right;
101+
next = _next;
102+
}
103+
};
104+
105+
// V0
106+
// public Node connect(Node root) {
107+
//
108+
// }
109+
110+
// V1
111+
// https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/solutions/5449615/my-easy-accepted-java-solution-100-by-di-nz2w/
112+
public Node connect_1(Node root) {
113+
if (root == null)
114+
return null;
115+
116+
Queue<Node> queue = new LinkedList<>();
117+
queue.offer(root);
118+
119+
while (!queue.isEmpty()) {
120+
int levelSize = queue.size();
121+
Node prev = null;
122+
123+
for (int i = 0; i < levelSize; i++) {
124+
Node currentNode = queue.poll();
125+
126+
if (prev != null) {
127+
prev.next = currentNode;
128+
}
129+
130+
prev = currentNode;
131+
132+
if (currentNode.left != null) {
133+
queue.offer(currentNode.left);
134+
}
135+
if (currentNode.right != null) {
136+
queue.offer(currentNode.right);
137+
}
138+
}
139+
}
140+
141+
return root;
142+
}
143+
144+
// V2-1
145+
// IDEA: BFS
146+
// https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/solutions/2033511/3-approaches-bfs-bfs-linkedlist-recursio-0q68/
147+
148+
// V2-2
149+
// IDEA: BFS + LINKEDLIST
150+
// https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/solutions/2033511/3-approaches-bfs-bfs-linkedlist-recursio-0q68/
151+
152+
// V2-3
153+
// IDEA: RECURSION
154+
// https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/solutions/2033511/3-approaches-bfs-bfs-linkedlist-recursio-0q68/
155+
}

0 commit comments

Comments
 (0)