forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fa84ac
commit 2fd101c
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
cmake_minimum_required(VERSION 3.14) | ||
project(C) | ||
|
||
set(CMAKE_CXX_STANDARD 14) | ||
|
||
add_executable(C main2.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/// Source : https://leetcode.com/problems/delete-nodes-and-return-forest/ | ||
/// Author : liuyubobobo | ||
/// Time : 2019-07-06 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
|
||
/// Simulations | ||
/// Time Complexity: O(|to_delete| * n) | ||
/// Space Complexity: O(h) | ||
|
||
/// Definition for a binary tree node. | ||
struct TreeNode { | ||
int val; | ||
TreeNode *left; | ||
TreeNode *right; | ||
TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) { | ||
|
||
vector<TreeNode*> res = {root}; | ||
for(int e: to_delete) | ||
res = delNodes(res, e); | ||
return res; | ||
} | ||
|
||
private: | ||
vector<TreeNode*> delNodes(vector<TreeNode*> v, int to_delete){ | ||
|
||
vector<TreeNode*> ret; | ||
bool ok = false; | ||
for(TreeNode* root: v) | ||
if(ok || !delNode(root, NULL, -1, root, to_delete, ret)) | ||
ret.push_back(root); | ||
else | ||
ok = true; | ||
|
||
return ret; | ||
} | ||
|
||
bool delNode(TreeNode* root, TreeNode* parent, int index, TreeNode* node, int to_delete, | ||
vector<TreeNode*>& ret){ | ||
|
||
if(!node) return false; | ||
if(node->val == to_delete){ | ||
if(node == root){ | ||
if(node->left) ret.push_back(node->left); | ||
if(node->right) ret.push_back(node->right); | ||
} | ||
else{ | ||
if(index == 0) parent->left = NULL; | ||
else parent->right = NULL; | ||
|
||
ret.push_back(root); | ||
if(node->left) ret.push_back(node->left); | ||
if(node->right) ret.push_back(node->right); | ||
} | ||
return true; | ||
} | ||
|
||
if(delNode(root, node, 0, node->left, to_delete, ret)) | ||
return true; | ||
return delNode(root, node, 1, node->right, to_delete, ret); | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
TreeNode* root = new TreeNode(1); | ||
root->left = new TreeNode(2); | ||
root->right = new TreeNode(3); | ||
root->left->left = new TreeNode(4); | ||
root->left->right = new TreeNode(5); | ||
root->right->left = new TreeNode(6); | ||
root->right->right = new TreeNode(7); | ||
|
||
vector<int> to_delete = {3, 5}; | ||
Solution().delNodes(root, to_delete); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/// Source : https://leetcode.com/problems/delete-nodes-and-return-forest/ | ||
/// Author : liuyubobobo | ||
/// Time : 2019-07-07 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <unordered_set> | ||
|
||
using namespace std; | ||
|
||
|
||
/// Recursion | ||
/// Time Complexity: O(n) | ||
/// Space Complexity: O(h) | ||
|
||
/// Definition for a binary tree node. | ||
struct TreeNode { | ||
int val; | ||
TreeNode *left; | ||
TreeNode *right; | ||
TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) { | ||
|
||
unordered_set<int> remove_set(to_delete.begin(), to_delete.end()); | ||
vector<TreeNode*> res; | ||
remove(root, true, remove_set, res); | ||
return res; | ||
} | ||
|
||
private: | ||
TreeNode* remove(TreeNode* node, bool is_root, | ||
const unordered_set<int>& remove_set, vector<TreeNode*>& res){ | ||
|
||
if(!node) return NULL; | ||
|
||
bool deleted = remove_set.count(node->val); | ||
if(is_root && !deleted) res.push_back(node); | ||
node->left = remove(node->left, deleted, remove_set, res); | ||
node->right = remove(node->right, deleted, remove_set, res); | ||
return deleted ? NULL : node; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
TreeNode* root = new TreeNode(1); | ||
root->left = new TreeNode(2); | ||
root->right = new TreeNode(3); | ||
root->left->left = new TreeNode(4); | ||
root->left->right = new TreeNode(5); | ||
root->right->left = new TreeNode(6); | ||
root->right->right = new TreeNode(7); | ||
|
||
vector<int> to_delete = {3, 5}; | ||
Solution().delNodes(root, to_delete); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -752,4 +752,5 @@ email: [[email protected]](mailto:[email protected]) | |
| | | | | | | | ||
| 1108 | [Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address/) | [无] | [C++](1108-Defanging-an-IP-Address/cpp-1108/) | | | | ||
| 1109 | [Corporate Flight Bookings](https://leetcode.com/problems/corporate-flight-bookings/) | [无] | [C++](1109-Corporate-Flight-Bookings/cpp-1009/) | | | | ||
| 1110 | [Delete Nodes And Return Forest](https://leetcode.com/problems/delete-nodes-and-return-forest/) | [无] | [C++](1110-Delete-Nodes-And-Return-Forest/cpp-1110/) | | | | ||
| | | | | | | |