Skip to content

Commit

Permalink
1110 added.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Jul 7, 2019
1 parent 2fa84ac commit 2fd101c
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 1110-Delete-Nodes-And-Return-Forest/cpp-1110/CMakeLists.txt
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)
88 changes: 88 additions & 0 deletions 1110-Delete-Nodes-And-Return-Forest/cpp-1110/main.cpp
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;
}
63 changes: 63 additions & 0 deletions 1110-Delete-Nodes-And-Return-Forest/cpp-1110/main2.cpp
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;
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/) | | |
| | | | | | |

0 comments on commit 2fd101c

Please sign in to comment.