|
| 1 | +--- |
| 2 | +id: delete-nodes-and-return-forest |
| 3 | +title: Delete Nodes And Return Forest |
| 4 | +sidebar_label: Delete Nodes And Return Forest |
| 5 | +tags: [Binary Tree, Depth-First Search, C++, Python, Java] |
| 6 | +description: Solve the problem of deleting nodes from a binary tree and returning the forest of remaining trees using depth-first search. |
| 7 | +--- |
| 8 | + |
| 9 | +## Problem Statement |
| 10 | + |
| 11 | +### Problem Description |
| 12 | + |
| 13 | +Given the root of a binary tree, each node in the tree has a distinct value. |
| 14 | + |
| 15 | +After deleting all nodes with a value in `to_delete`, we are left with a forest (a disjoint union of trees). |
| 16 | + |
| 17 | +Return the roots of the trees in the remaining forest. You may return the result in any order. |
| 18 | + |
| 19 | +### Example |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | +``` |
| 23 | +Input: root = [1, 2, 3, 4, 5, 6, 7], to_delete = [3, 5] |
| 24 | +Output: [[1, 2, null, 4], [6], [7]] |
| 25 | +``` |
| 26 | + |
| 27 | +### Constraints |
| 28 | + |
| 29 | +- The number of nodes in the given tree is at most 1000. |
| 30 | +- Each node has a distinct value between 1 and 1000. |
| 31 | +- `to_delete` contains distinct values between 1 and 1000. |
| 32 | + |
| 33 | +## Solution |
| 34 | + |
| 35 | +### Intuition |
| 36 | + |
| 37 | +The problem can be solved using a depth-first search (DFS) approach. The idea is to traverse the tree and, whenever a node needs to be deleted (i.e., its value is in the `to_delete` list), we handle its children by potentially adding them to the forest if they are not to be deleted. We maintain a set of nodes to be deleted for quick lookup and use a helper function to perform the DFS and manage the forest creation. |
| 38 | + |
| 39 | +### Time Complexity and Space Complexity Analysis |
| 40 | + |
| 41 | +- **Time Complexity**: |
| 42 | + - The solution involves a single traversal of the tree, making the time complexity $O(n)$, where `n` is the number of nodes in the tree. |
| 43 | + |
| 44 | +- **Space Complexity**: |
| 45 | + - The space complexity is $O(n)$ due to the recursive call stack and storage for the result list. |
| 46 | + |
| 47 | +### Code |
| 48 | + |
| 49 | +#### C++ |
| 50 | + |
| 51 | +```cpp |
| 52 | +class Solution { |
| 53 | +public: |
| 54 | + vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) { |
| 55 | + unordered_set<int> to_delete_set(to_delete.begin(), to_delete.end()); |
| 56 | + vector<TreeNode*> forest; |
| 57 | + if (!to_delete_set.count(root->val)) { |
| 58 | + forest.push_back(root); |
| 59 | + } |
| 60 | + dfs(root, to_delete_set, forest); |
| 61 | + return forest; |
| 62 | + } |
| 63 | + |
| 64 | +private: |
| 65 | + TreeNode* dfs(TreeNode* node, unordered_set<int>& to_delete_set, vector<TreeNode*>& forest) { |
| 66 | + if (!node) return nullptr; |
| 67 | + node->left = dfs(node->left, to_delete_set, forest); |
| 68 | + node->right = dfs(node->right, to_delete_set, forest); |
| 69 | + if (to_delete_set.count(node->val)) { |
| 70 | + if (node->left) forest.push_back(node->left); |
| 71 | + if (node->right) forest.push_back(node->right); |
| 72 | + return nullptr; |
| 73 | + } |
| 74 | + return node; |
| 75 | + } |
| 76 | +}; |
| 77 | +``` |
| 78 | +#### Python |
| 79 | +```python |
| 80 | +class Solution: |
| 81 | + def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]: |
| 82 | + seats = [0] * (n + 1) |
| 83 | + for booking in bookings: |
| 84 | + first, last, seats_count = booking |
| 85 | + seats[first - 1] += seats_count |
| 86 | + if last < n: |
| 87 | + seats[last] -= seats_count |
| 88 | + for i in range(1, n): |
| 89 | + seats[i] += seats[i - 1] |
| 90 | + return seats[:-1] |
| 91 | +
|
| 92 | +``` |
| 93 | +#### Java |
| 94 | +```java |
| 95 | +class Solution { |
| 96 | + public int[] corpFlightBookings(int[][] bookings, int n) { |
| 97 | + int[] seats = new int[n + 1]; |
| 98 | + for (int[] booking : bookings) { |
| 99 | + int first = booking[0]; |
| 100 | + int last = booking[1]; |
| 101 | + int seatsCount = booking[2]; |
| 102 | + seats[first - 1] += seatsCount; |
| 103 | + if (last < n) { |
| 104 | + seats[last] -= seatsCount; |
| 105 | + } |
| 106 | + } |
| 107 | + for (int i = 1; i < n; i++) { |
| 108 | + seats[i] += seats[i - 1]; |
| 109 | + } |
| 110 | + return Arrays.copyOf(seats, n); |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
0 commit comments