From 2fd101c5759042341096b72bdb3f594dccf4dbc6 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 7 Jul 2019 11:04:49 -0700 Subject: [PATCH] 1110 added. --- .../cpp-1110/CMakeLists.txt | 6 ++ .../cpp-1110/main.cpp | 88 +++++++++++++++++++ .../cpp-1110/main2.cpp | 63 +++++++++++++ readme.md | 1 + 4 files changed, 158 insertions(+) create mode 100644 1110-Delete-Nodes-And-Return-Forest/cpp-1110/CMakeLists.txt create mode 100644 1110-Delete-Nodes-And-Return-Forest/cpp-1110/main.cpp create mode 100644 1110-Delete-Nodes-And-Return-Forest/cpp-1110/main2.cpp diff --git a/1110-Delete-Nodes-And-Return-Forest/cpp-1110/CMakeLists.txt b/1110-Delete-Nodes-And-Return-Forest/cpp-1110/CMakeLists.txt new file mode 100644 index 00000000..70268b97 --- /dev/null +++ b/1110-Delete-Nodes-And-Return-Forest/cpp-1110/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.14) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main2.cpp) \ No newline at end of file diff --git a/1110-Delete-Nodes-And-Return-Forest/cpp-1110/main.cpp b/1110-Delete-Nodes-And-Return-Forest/cpp-1110/main.cpp new file mode 100644 index 00000000..131d2f75 --- /dev/null +++ b/1110-Delete-Nodes-And-Return-Forest/cpp-1110/main.cpp @@ -0,0 +1,88 @@ +/// Source : https://leetcode.com/problems/delete-nodes-and-return-forest/ +/// Author : liuyubobobo +/// Time : 2019-07-06 + +#include +#include + +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 delNodes(TreeNode* root, vector& to_delete) { + + vector res = {root}; + for(int e: to_delete) + res = delNodes(res, e); + return res; + } + +private: + vector delNodes(vector v, int to_delete){ + + vector 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& 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 to_delete = {3, 5}; + Solution().delNodes(root, to_delete); + + return 0; +} \ No newline at end of file diff --git a/1110-Delete-Nodes-And-Return-Forest/cpp-1110/main2.cpp b/1110-Delete-Nodes-And-Return-Forest/cpp-1110/main2.cpp new file mode 100644 index 00000000..d592b78d --- /dev/null +++ b/1110-Delete-Nodes-And-Return-Forest/cpp-1110/main2.cpp @@ -0,0 +1,63 @@ +/// Source : https://leetcode.com/problems/delete-nodes-and-return-forest/ +/// Author : liuyubobobo +/// Time : 2019-07-07 + +#include +#include +#include + +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 delNodes(TreeNode* root, vector& to_delete) { + + unordered_set remove_set(to_delete.begin(), to_delete.end()); + vector res; + remove(root, true, remove_set, res); + return res; + } + +private: + TreeNode* remove(TreeNode* node, bool is_root, + const unordered_set& remove_set, vector& 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 to_delete = {3, 5}; + Solution().delNodes(root, to_delete); + + return 0; +} \ No newline at end of file diff --git a/readme.md b/readme.md index 9649cfd2..12bec6cc 100644 --- a/readme.md +++ b/readme.md @@ -752,4 +752,5 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 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/) | | | | | | | | | | \ No newline at end of file