Skip to content

Commit

Permalink
add 1110 java code comment
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Aug 31, 2024
1 parent 07a4598 commit de3252f
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ public class DeleteNodesAndReturnForest {
// V1
// IDEA : BFS
// https://leetcode.com/problems/delete-nodes-and-return-forest/solutions/5489867/explanations-no-one-will-give-you-2-detailed-approaches-extremely-simple-and-effective/
/**
* NOTE !!!
*
* 1. return type is List<TreeNode>,
* so we just need to return "parent node" of each node after delete operation
*
* 2. main point :
* - handle to-delete node and its sub tree
* - collect node when node is/isn't in to-delete list
*
* 3. Why Nodes Aren’t Missed:
* - Root Node: If the root is not deleted, it is added to the forest. If it is deleted, any non-null children of the root will be added to the forest as new tree roots during the BFS traversal.
* - Other Nodes: During the BFS traversal, every node is processed. If a node is not in the toDeleteSet, it remains connected to its parent. If the node is in the toDeleteSet, its children (if any) are added to the forest.
*
*/
public List<TreeNode> delNodes_1(TreeNode root, int[] to_delete) {
List<TreeNode> result = new ArrayList<>();
if (root == null) return result;
Expand Down Expand Up @@ -71,6 +86,32 @@ public List<TreeNode> delNodes_1(TreeNode root, int[] to_delete) {
// V2
// IDEA : BFS Forest Formation
// https://leetcode.com/problems/delete-nodes-and-return-forest/editorial/
/**
* NOTE !!! (by GPT)
*
* why we don't miss any nodes that are not in the toDeleteSet
* when forest.add(root); is outside of wheile loop
*
* 1. Handling the Root Node:
* • The root node is special because it’s the starting point of the tree. If the root node is not in the toDeleteSet, it means the entire original tree (minus any deleted nodes) should be part of the resulting forest. Therefore, adding the root node to the forest outside the loop is correct.
*
* 2. Processing All Other Nodes:
* • Inside the while loop, the BFS traversal ensures that every node in the tree is visited.
* • For each node that needs to be deleted (i.e., if it’s in the toDeleteSet), its non-null children are added to the forest if they exist. This ensures that any subtree roots formed by deleting nodes are included in the forest.
* • For nodes that are not in the toDeleteSet, they remain part of the tree rooted at the original root, and since the root is checked outside the loop, the entire connected component of nodes is correctly handled.
*
* 3. Why Nodes Aren’t Missed:
* • Root Node: If the root is not deleted, it is added to the forest. If it is deleted, any non-null children of the root will be added to the forest as new tree roots during the BFS traversal.
* • Other Nodes: During the BFS traversal, every node is processed. If a node is not in the toDeleteSet, it remains connected to its parent. If the node is in the toDeleteSet, its children (if any) are added to the forest.
*
* Conclusion:
*
* - The check outside the loop for the root node ensures that the
* root (and any connected nodes not in the toDeleteSet)
* is correctly added to the forest.
* Since BFS covers all nodes, no nodes are missed.
*
*/
public List<TreeNode> delNodes_2(TreeNode root, int[] to_delete) {
if (root == null) {
return new ArrayList<>();
Expand Down

0 comments on commit de3252f

Please sign in to comment.