Skip to content

Commit 5eaf50b

Browse files
committed
Updated Chp.2-4 solutions
1 parent c4fcb48 commit 5eaf50b

File tree

12 files changed

+33
-32
lines changed

12 files changed

+33
-32
lines changed

Chp. 03 - Stacks and Queues/_3_5_Sort_Stack/SortStack.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
import java.util.Stack;
44

5-
// Time Complexity: O(n^2)
6-
// Space Complexity: O(n)
7-
85
public class SortStack {
96
public static Stack<Integer> sort(Stack<Integer> stack) {
107
Stack<Integer> helperStack = new Stack<>(); // can alternatively use ArrayDeque (it's faster)
@@ -18,3 +15,6 @@ public static Stack<Integer> sort(Stack<Integer> stack) {
1815
return helperStack;
1916
}
2017
}
18+
19+
// Time Complexity: O(n^2)
20+
// Space Complexity: O(n)

Chp. 04 - Trees and Graphs/_4_01_Route_Between_Nodes/RouteBetweenNodes.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import common.GraphNode;
55

66
// Algorithm: run BFS from start node and see if we arrive at end node
7-
// Improvement: Do bi-directional BFS to improve runtime
87

98
public class RouteBetweenNodes {
109
public static boolean routeExists(GraphNode start, GraphNode end) {
@@ -31,3 +30,5 @@ public static boolean routeExists(GraphNode start, GraphNode end) {
3130
return false;
3231
}
3332
}
33+
34+
// Improvement: Do bi-directional BFS to improve runtime

Chp. 04 - Trees and Graphs/_4_03_List_of_Depths/ListOfDepths.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,14 @@
44
import java.util.LinkedList;
55
import common.TreeNode;
66

7-
// Tricky Skills:
8-
// 1) Knowing I should return an "ArrayList<LinkedList<Node>>".
9-
// 2) Knowing that, to alter the "ArrayList<LinkedList<Node>>", I should be passing it as a parameter so it can be altered.
10-
// 3) Know to also pass the "level" down the tree.
11-
// 4) Remembering to create new LinkedLists in our ArrayList when necessary.
12-
// 5) Making a DEEP COPY whenever we add Nodes to the list.
13-
147
public class ListOfDepths {
158
public static ArrayList<LinkedList<TreeNode>> createLists(TreeNode root) {
169
ArrayList<LinkedList<TreeNode>> lists = new ArrayList<>();
1710
createListsHelper(root, lists, 0);
1811
return lists;
1912
}
2013

21-
public static void createListsHelper(TreeNode node, ArrayList<LinkedList<TreeNode>> lists, int currLevel) {
14+
private static void createListsHelper(TreeNode node, ArrayList<LinkedList<TreeNode>> lists, int currLevel) {
2215
if (node == null) {
2316
return;
2417
}
@@ -37,3 +30,10 @@ public static void createListsHelper(TreeNode node, ArrayList<LinkedList<TreeNod
3730
createListsHelper(node.right, lists, currLevel + 1);
3831
}
3932
}
33+
34+
// Tricky Implementation Details:
35+
// 1) Knowing to return an "ArrayList<LinkedList<Node>>".
36+
// 2) Knowing that, to alter the "ArrayList<LinkedList<Node>>", we should pass it as a parameter so it can be altered.
37+
// 3) Know to also pass the "level" down the tree.
38+
// 4) Remembering to create new LinkedLists in our ArrayList when necessary.
39+
// 5) Making a DEEP COPY whenever we add Nodes to the list.

Chp. 04 - Trees and Graphs/_4_04_Check_Balanced/CheckBalanced.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
// Algorithm: If subtree at Node is imbalanced, return -1. Otherwise, return height of subtree.
66

7-
// Time Complexity: O(n)
8-
// Space Complexity: O(h)
9-
107
public class CheckBalanced {
118
public static boolean isBalanced(TreeNode root) {
129
return (isBalancedHelper(root) == -1) ? false : true;
@@ -34,3 +31,6 @@ private static int isBalancedHelper(TreeNode root) {
3431
return 1 + Math.max(leftHeight, rightHeight);
3532
}
3633
}
34+
35+
// Time Complexity: O(n)
36+
// Space Complexity: O(n)

Chp. 04 - Trees and Graphs/_4_05_Validate_BST/ValidateBST.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
import common.TreeNode;
44

5-
// Time Complexity: O(n) - since we visit every node
6-
// Space Complexity: O(log n) - that's the depth of the recursion
7-
85
public class ValidateBST {
96
public static boolean isBST(TreeNode root) {
107
return isBST_Helper(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
@@ -20,3 +17,6 @@ private static boolean isBST_Helper(TreeNode node, int min, int max) {
2017
return isBST_Helper(node.left, min, node.data) && isBST_Helper(node.right, node.data, max);
2118
}
2219
}
20+
21+
// Time Complexity: O(n) - since we visit every node
22+
// Space Complexity: O(log n) - that's the depth of the recursion

Chp. 04 - Trees and Graphs/_4_06_Successor/Successor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import common.TreeNode;
44

5-
// Read book's logic, but ignore its code. My code is much cleaner.
5+
// Read book's logic, but ignore its code. The code below is much cleaner.
66
// - Notice there are 2 distinct cases: "leftMostChild" (easy to code), "properParent" (a little tricky)
77

88
public class Successor {

Chp. 04 - Trees and Graphs/_4_07_Build_Order/Node.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
class Node {
66
public char data;
77
public Visited status;
8-
public ArrayList<Node> neighbors; // could alternatively use a HashSet (if I
9-
// give nodes unique IDs)
8+
public ArrayList<Node> neighbors; // could alternatively use a HashSet (if I give nodes unique IDs)
109

1110
/* Constructor */
1211
public Node(char data) {

Chp. 04 - Trees and Graphs/_4_08_First_Common_Ancestor/FirstCommonAncestor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static TreeNode commonAnc(TreeNode root, TreeNode p, TreeNode q) {
2424
return root;
2525
}
2626

27-
TreeNode left = commonAnc(root.left, p, q);
27+
TreeNode left = commonAnc(root.left, p, q);
2828
TreeNode right = commonAnc(root.right, p, q);
2929

3030
if (left == null) {

Chp. 04 - Trees and Graphs/_4_12_Paths_with_Sum/PathWithSums.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33
import java.util.ArrayList;
44
import common.TreeNode;
55

6-
// Big Trick: Instead of checking if a node starts a path that sums to "value", we check if it ENDS a path
7-
// summing to "value".
6+
// The Big Trick
87
//
9-
// Time/Space complexities: Assuming BALANCED binary tree
10-
// Time complexity: O(n log(n)) since we touch all "n" nodes, and findSum is O(log n)) in AVERAGE case.
11-
// findSum worst case is O((log n)^2) since it may call print every single time,
12-
// making overall time complexity worst case O(n log(n)^2)
13-
// Space complexity: O(log n) since that's the max size of a path, and also the max amount of recursive calls on stack.
8+
// Instead of checking if a node starts a path that sums to "value", we check if it ENDS a path summing to "value".
149

1510
public class PathWithSums {
1611
public static void findSum(TreeNode node, int value) {
@@ -47,3 +42,9 @@ private static void printPath(ArrayList<TreeNode> path, int start, int end) {
4742
}
4843
}
4944
}
45+
46+
// Time/Space complexities: Assuming BALANCED binary tree
47+
// Time complexity: O(n log(n)) since we touch all "n" nodes, and findSum is O(log n)) in AVERAGE case.
48+
// findSum worst case is O((log n)^2) since it may call print every single time,
49+
// making overall time complexity worst case O(n (log n)^2)
50+
// Space complexity: O(log n) since that's the max size of a path, and also the max amount of recursive calls on stack.

Common/common/GraphFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static void DFS(GraphNode node, int data) {
2929
}
3030

3131
// Don't forget:
32-
// 1) Use a Queue
32+
// 1) Use a Queue (or ArrayDeque as a Queue)
3333
// 2) .visit() a node before we add it to deque (to avoid duplicates on deque)
3434
public static void BFS(GraphNode node, int data) {
3535
if (node == null) {

Common/common/GraphNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class GraphNode {
99
public int data;
1010
public boolean visited; // needed for BFS, DFS
11-
private ArrayList<GraphNode> neighbors; // could alternatively use a HashSet (and give nodes unique IDs)
11+
private ArrayList<GraphNode> neighbors; // can alternatively use a HashSet (and give nodes unique IDs)
1212

1313
/* Constructor */
1414
public GraphNode(int data) {

Common/common/TreeNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package common;
22

3-
// Represents a binary Tree. If we wanted an n-ary tree,
3+
// This represents a binary Tree. If we wanted an n-ary tree,
44
// we would use an array of TreeNodes as children
55

66
public class TreeNode {

0 commit comments

Comments
 (0)