From 5def2db17a1f039bf080c1aa02e9f4bfec967e67 Mon Sep 17 00:00:00 2001 From: Abinash Karmakar Date: Wed, 30 Aug 2023 16:21:22 +0530 Subject: [PATCH] Added some code --- .../binary-tree-level-order-traversal-ii.cpp | 86 +++++++++++++++++++ ...ert-sorted-array-to-binary-search-tree.cpp | 33 +++++++ ...vert-sorted-list-to-binary-search-tree.cpp | 50 +++++++++++ 3 files changed, 169 insertions(+) create mode 100644 leetcode/problems/cpp/binary-tree-level-order-traversal-ii.cpp create mode 100644 leetcode/problems/cpp/convert-sorted-array-to-binary-search-tree.cpp create mode 100644 leetcode/problems/cpp/convert-sorted-list-to-binary-search-tree.cpp diff --git a/leetcode/problems/cpp/binary-tree-level-order-traversal-ii.cpp b/leetcode/problems/cpp/binary-tree-level-order-traversal-ii.cpp new file mode 100644 index 0000000..92c70e3 --- /dev/null +++ b/leetcode/problems/cpp/binary-tree-level-order-traversal-ii.cpp @@ -0,0 +1,86 @@ +// by @codeAbinash +// Time : O(n) +// Space : O(n) + +#include "iostream" +#include "vector" +#include "stack" +#include "queue" +#include "algorithm" +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) {} +}; + + +class Solution { + +public: + vector> levelOrderBottom(TreeNode* root) { + vector> ans; + if (!root) return ans; + + queue q; + q.push(root); + + vector tmp; + while (!q.empty()) { + int size = q.size(); + while (size--) { + TreeNode* front = q.front(); + q.pop(); + tmp.push_back(front->val); + if (front->left) q.push(front->left); + if (front->right) q.push(front->right); + } + ans.push_back(tmp); + tmp.clear(); + } + + reverse(ans.begin(), ans.end()); + return ans; + } +}; + + +// Using a Stack + +class Solution { + +public: + vector> levelOrderBottom(TreeNode* root) { + vector> ans; + stack> stk; + if (!root) return ans; + + queue q; + q.push(root); + + vector tmp; + while (!q.empty()) { + int size = q.size(); + while (size--) { + TreeNode* front = q.front(); + q.pop(); + tmp.push_back(front->val); + if (front->left) q.push(front->left); + if (front->right) q.push(front->right); + } + stk.push(tmp); + tmp.clear(); + } + + while (!stk.empty()) { + ans.push_back(stk.top()); + stk.pop(); + } + return ans; + } +}; + diff --git a/leetcode/problems/cpp/convert-sorted-array-to-binary-search-tree.cpp b/leetcode/problems/cpp/convert-sorted-array-to-binary-search-tree.cpp new file mode 100644 index 0000000..3a24aca --- /dev/null +++ b/leetcode/problems/cpp/convert-sorted-array-to-binary-search-tree.cpp @@ -0,0 +1,33 @@ +// by @codeAbinash +// Time : O(n) +// Space : O(n) + +#include "iostream" +#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) {} +}; + + +class Solution { + TreeNode* makeTree(int left, int right, vector& nums) { + if (left >= right) return NULL; + int mid = (left + right) / 2; + TreeNode* newNode = new TreeNode(nums[mid]); + newNode->left = makeTree(left, mid, nums); + newNode->right = makeTree(mid + 1, right, nums); + return newNode; + } +public: + TreeNode* sortedArrayToBST(vector& nums) { + return makeTree(0, nums.size(), nums); + } +}; \ No newline at end of file diff --git a/leetcode/problems/cpp/convert-sorted-list-to-binary-search-tree.cpp b/leetcode/problems/cpp/convert-sorted-list-to-binary-search-tree.cpp new file mode 100644 index 0000000..c3f3f69 --- /dev/null +++ b/leetcode/problems/cpp/convert-sorted-list-to-binary-search-tree.cpp @@ -0,0 +1,50 @@ +// by @codeAbinash +// Time : O(n) +// Space : O(n) + +#include "iostream" +#include "vector" + + + +struct ListNode { + int val; + ListNode* next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode* next) : val(x), next(next) {} +}; + + +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) {} +}; + +class Solution { + TreeNode* toBst(ListNode* start, ListNode* end) { + if (start == end) return nullptr; + ListNode* slow = start; + ListNode* fast = start; + + while (fast != end && fast->next != end) { + slow = slow->next; + fast = fast->next->next; + } + + TreeNode* head = new TreeNode(slow->val); + head->left = toBst(start, slow); + head->right = toBst(slow->next, end); + return head; + } + +public: + TreeNode* sortedListToBST(ListNode* head) { + if (!head) return nullptr; + return toBst(head, nullptr); + } +}; \ No newline at end of file