-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
143 lines (121 loc) · 3.9 KB
/
main.cpp
File metadata and controls
143 lines (121 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Source: https://leetcode.com/problems/delete-nodes-and-return-forest
// Title: Delete Nodes And Return Forest
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given the `root` of a binary tree, each node in the tree has a distinct value.
//
// After deleting all nodes with a value in `to_delete`, we are left with a forest (a disjoint union of trees).
//
// Return the roots of the trees in the remaining forest. You may return the result in any order.
//
// **Example 1:**
// https://assets.leetcode.com/uploads/2019/07/01/screen-shot-2019-07-01-at-53836-pm.png
//
// ```
// Input: root = [1,2,3,4,5,6,7], to_delete = [3,5]
// Output: [[1,2,null,4],[6],[7]]
// ```
//
// **Example 2:**
//
// ```
// Input: root = [1,2,4,null,3], to_delete = [3]
// Output: [[1,2,4]]
// ```
//
// **Constraints:**
//
// - The number of nodes in the given tree is at most `1000`.
// - Each node has a distinct value between `1` and `1000`.
// - `to_delete.length <= 1000`
// - `to_delete` contains distinct values between `1` and `1000`.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <unordered_set>
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
// Hash Set + DFS
//
// First convert toDelete into a hash set.
// Next do DFS on the tree.
// If a node it deleteing, tell its children to put themself into the forest
class Solution {
public:
vector<TreeNode *> delNodes(TreeNode *root, const vector<int> &toDelete) {
// Trivial case
if (toDelete.empty()) return {root};
// Prepare data
auto deleteSet = unordered_set<int>(toDelete.cbegin(), toDelete.cend());
auto forest = vector<TreeNode *>();
// DFS
dfs(root, true, deleteSet, forest);
return forest;
}
private:
// Returns updated node
TreeNode *dfs(TreeNode *node, bool parentDeleted, const unordered_set<int> &deleteSet, vector<TreeNode *> &forest) {
// Empty node
if (!node) return nullptr;
// Delete current node
bool currentDeleted = deleteSet.contains(node->val);
// Traversal
node->left = dfs(node->left, currentDeleted, deleteSet, forest);
node->right = dfs(node->right, currentDeleted, deleteSet, forest);
// Delete node
if (currentDeleted) {
delete node;
return nullptr;
}
// Put into forest
if (parentDeleted) {
forest.push_back(node);
}
return node;
}
};
// Hash Set + DFS
//
// First convert toDelete into a hash set.
// Next do post-order DFS on the tree.
// If a node is deleting, then put its non-nil children into the forest
class Solution2 {
public:
vector<TreeNode *> delNodes(TreeNode *root, vector<int> &toDelete) {
// Trivial case
if (toDelete.empty()) return {root};
// Prepare data
auto deleteSet = unordered_set<int>(toDelete.cbegin(), toDelete.cend());
auto forest = vector<TreeNode *>();
// DFS and check root node
if (dfs(root, deleteSet, forest)) {
forest.push_back(root);
}
return forest;
}
private:
// Returns updated node
TreeNode *dfs(TreeNode *node, const unordered_set<int> &deleteSet, vector<TreeNode *> &forest) {
// Empty node
if (!node) return nullptr;
// Traversal
node->left = dfs(node->left, deleteSet, forest);
node->right = dfs(node->right, deleteSet, forest);
// Delete node
if (deleteSet.contains(node->val)) {
if (node->left) forest.push_back(node->left);
if (node->right) forest.push_back(node->right);
delete node;
return nullptr;
}
return node;
}
};