You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/g0101_0200/s0102_binary_tree_level_order_traversal/readme.md
+74-1
Original file line number
Diff line number
Diff line change
@@ -27,4 +27,77 @@ Given the `root` of a binary tree, return _the level order traversal of its node
27
27
**Constraints:**
28
28
29
29
* The number of nodes in the tree is in the range `[0, 2000]`.
30
-
*`-1000 <= Node.val <= 1000`
30
+
*`-1000 <= Node.val <= 1000`
31
+
32
+
To solve this problem in Java with a `Solution` class, we'll use a breadth-first search (BFS) traversal approach. Below are the steps:
33
+
34
+
1.**Create a `Solution` class**: Define a class named `Solution` to encapsulate our solution methods.
35
+
36
+
2.**Create a `levelOrder` method**: This method takes the root node of the binary tree as input and returns the level order traversal of its nodes' values.
37
+
38
+
3.**Check for null root**: Check if the root is null. If so, return an empty list since there are no nodes to traverse.
39
+
40
+
4.**Initialize a queue**: We'll use a queue to perform BFS traversal. Add the root node to the queue.
41
+
42
+
5.**Traverse the tree level by level**:
43
+
- Start a loop that continues until the queue is empty.
44
+
- Inside the loop, for each level, get the size of the queue (number of nodes at the current level).
45
+
- Create a list to store the node values at the current level.
46
+
- Iterate through all nodes at the current level:
47
+
- Poll a node from the queue.
48
+
- Add its value to the list.
49
+
- If the node has left or right children, add them to the queue.
50
+
- After processing all nodes at the current level, add the list of node values to the result list.
51
+
52
+
6.**Return the result**: After traversing the entire tree, return the result containing the level order traversal.
Copy file name to clipboardExpand all lines: src/main/java/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md
+77-1
Original file line number
Diff line number
Diff line change
@@ -26,4 +26,80 @@ Given two integer arrays `preorder` and `inorder` where `preorder` is the preord
26
26
*`preorder` and `inorder` consist of **unique** values.
27
27
* Each value of `inorder` also appears in `preorder`.
28
28
*`preorder` is **guaranteed** to be the preorder traversal of the tree.
29
-
*`inorder` is **guaranteed** to be the inorder traversal of the tree.
29
+
*`inorder` is **guaranteed** to be the inorder traversal of the tree.
30
+
31
+
To solve the "Construct Binary Tree from Preorder and Inorder Traversal" problem in Java with a `Solution` class, we'll use a recursive approach. Below are the steps:
32
+
33
+
1.**Create a `Solution` class**: Define a class named `Solution` to encapsulate our solution methods.
34
+
35
+
2.**Create a `buildTree` method**: This method takes two integer arrays, `preorder` and `inorder`, as input and returns the constructed binary tree.
36
+
37
+
3.**Check for empty arrays**: Check if either of the arrays `preorder` or `inorder` is empty. If so, return null, as there's no tree to construct.
38
+
39
+
4.**Define a helper method**: Define a recursive helper method `build` to construct the binary tree.
40
+
- The method should take the indices representing the current subtree in both `preorder` and `inorder`.
41
+
- The start and end indices in `preorder` represent the current subtree's preorder traversal.
42
+
- The start and end indices in `inorder` represent the current subtree's inorder traversal.
43
+
44
+
5.**Base case**: If the start index of `preorder` is greater than the end index or if the start index of `inorder` is greater than the end index, return null.
45
+
46
+
6.**Find the root node**: The root node is the first element in the `preorder` array.
47
+
48
+
7.**Find the root's position in `inorder`**: Iterate through the `inorder` array to find the root's position.
49
+
50
+
8.**Recursively build left and right subtrees**:
51
+
- Recursively call the `build` method for the left subtree with updated indices.
52
+
- Recursively call the `build` method for the right subtree with updated indices.
53
+
54
+
9.**Return the root node**: After constructing the left and right subtrees, return the root node.
Copy file name to clipboardExpand all lines: src/main/java/g0101_0200/s0114_flatten_binary_tree_to_linked_list/readme.md
+78-1
Original file line number
Diff line number
Diff line change
@@ -32,4 +32,81 @@ Given the `root` of a binary tree, flatten the tree into a "linked list":
32
32
* The number of nodes in the tree is in the range `[0, 2000]`.
33
33
*`-100 <= Node.val <= 100`
34
34
35
-
**Follow up:** Can you flatten the tree in-place (with `O(1)` extra space)?
35
+
**Follow up:** Can you flatten the tree in-place (with `O(1)` extra space)?
36
+
37
+
To solve the "Flatten Binary Tree to Linked List" problem in Java with a `Solution` class, we'll use a recursive approach. Below are the steps:
38
+
39
+
1.**Create a `Solution` class**: Define a class named `Solution` to encapsulate our solution methods.
40
+
41
+
2.**Create a `flatten` method**: This method takes the root node of the binary tree as input and flattens the tree into a linked list using preorder traversal.
42
+
43
+
3.**Check for null root**: Check if the root is null. If so, there's no tree to flatten, so return.
44
+
45
+
4.**Recursively flatten the tree**: Define a recursive helper method `flattenTree` to perform the flattening.
46
+
- The method should take the current node as input.
47
+
- Perform a preorder traversal of the tree.
48
+
- For each node, if it has a left child:
49
+
- Find the rightmost node in the left subtree.
50
+
- Attach the right subtree of the current node to the right of the rightmost node.
51
+
- Move the left subtree to the right subtree position.
52
+
- Set the left child of the current node to null.
53
+
- Recursively call the method for the right child.
54
+
55
+
5.**Call the helper method**: Call the `flattenTree` method with the root node.
56
+
57
+
Here's the Java implementation:
58
+
59
+
```java
60
+
classSolution {
61
+
publicvoidflatten(TreeNoderoot) {
62
+
if (root ==null) return; // Check for empty tree
63
+
flattenTree(root); // Flatten the tree
64
+
}
65
+
66
+
// Recursive helper method to flatten the tree
67
+
privatevoidflattenTree(TreeNodenode) {
68
+
if (node ==null) return;
69
+
70
+
// Flatten left subtree
71
+
flattenTree(node.left);
72
+
73
+
// Flatten right subtree
74
+
flattenTree(node.right);
75
+
76
+
// Save right subtree
77
+
TreeNode rightSubtree = node.right;
78
+
79
+
// Attach left subtree to the right of the current node
80
+
node.right = node.left;
81
+
82
+
// Set left child to null
83
+
node.left =null;
84
+
85
+
// Move to the rightmost node of the flattened left subtree
86
+
TreeNode current = node;
87
+
while (current.right !=null) {
88
+
current = current.right;
89
+
}
90
+
91
+
// Attach the saved right subtree to the right of the rightmost node
92
+
current.right = rightSubtree;
93
+
}
94
+
95
+
// TreeNode definition
96
+
publicclassTreeNode {
97
+
int val;
98
+
TreeNode left;
99
+
TreeNode right;
100
+
101
+
TreeNode() {}
102
+
TreeNode(intval) { this.val = val; }
103
+
TreeNode(intval, TreeNodeleft, TreeNoderight) {
104
+
this.val = val;
105
+
this.left = left;
106
+
this.right = right;
107
+
}
108
+
}
109
+
}
110
+
```
111
+
112
+
This implementation follows the steps outlined above and efficiently flattens the binary tree into a linked list using preorder traversal in Java.
To solve the "Best Time to Buy and Sell Stock" problem in Java with a `Solution` class, we'll use a greedy algorithm. Below are the steps:
33
+
34
+
1.**Create a `Solution` class**: Define a class named `Solution` to encapsulate our solution methods.
35
+
36
+
2.**Create a `maxProfit` method**: This method takes an array `prices` as input and returns the maximum profit that can be achieved by buying and selling the stock.
37
+
38
+
3.**Initialize variables**: Initialize two variables, `minPrice` to store the minimum price seen so far and `maxProfit` to store the maximum profit seen so far. Initialize `maxProfit` to 0.
39
+
40
+
4.**Iterate through the prices array**:
41
+
- For each price in the array:
42
+
- Update `minPrice` to the minimum of the current price and `minPrice`.
43
+
- Update `maxProfit` to the maximum of the current profit (price - `minPrice`) and `maxProfit`.
44
+
45
+
5.**Return the maximum profit**: After iterating through the entire array, return `maxProfit`.
46
+
47
+
Here's the Java implementation:
48
+
49
+
```java
50
+
classSolution {
51
+
publicintmaxProfit(int[] prices) {
52
+
if (prices ==null|| prices.length <=1) return0; // Check for empty array or single element
53
+
54
+
int minPrice = prices[0]; // Initialize minPrice to the first price
This implementation follows the steps outlined above and efficiently calculates the maximum profit that can be achieved by buying and selling the stock in Java.
0 commit comments