-
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.
Added Construct Binary Tree from Preorder and Inorder Traversal
- Loading branch information
1 parent
cdd54fc
commit 7e6cacc
Showing
5 changed files
with
103 additions
and
2 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
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
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
80 changes: 80 additions & 0 deletions
80
Medium/ConstructBinaryTreeFromPreorderAndInorderTraversal/include/solution.hpp
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,80 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <gtest/gtest.h> | ||
|
||
struct TreeNode { | ||
const int32_t value; | ||
TreeNode *left; | ||
TreeNode *right; | ||
explicit TreeNode(int32_t value) : | ||
value(value), left(nullptr), right(nullptr) { } | ||
}; | ||
|
||
class Solution { | ||
public: | ||
static auto build_tree(const std::vector<int32_t> &preorder, | ||
const std::vector<int32_t> &inorder) -> TreeNode* { | ||
auto pre = preorder.begin(); | ||
auto in = inorder.begin(); | ||
|
||
std::function<TreeNode*(int32_t)> construct = [&](int32_t stop) -> TreeNode* { | ||
if (pre == preorder.end() || *in == stop) return (TreeNode*)nullptr; | ||
auto *node = new TreeNode{*(pre++)}; | ||
node->left = construct(node->value); | ||
assert (*in == node->value); | ||
in++; | ||
node->right = construct(stop); | ||
return node; | ||
}; | ||
|
||
return construct(std::numeric_limits<int32_t>::max()); | ||
} | ||
|
||
[[maybe_unused]] static auto build_tree_tle(const std::vector<int32_t> &preorder, | ||
const std::vector<int32_t> &inorder) -> TreeNode* { | ||
std::unordered_map<int32_t, int32_t> inorder_map; | ||
for (int i = 0; i < inorder.size(); ++i) { | ||
inorder_map[inorder[i]] = i; | ||
} | ||
|
||
return helper(preorder, 0, preorder.size() - 1, | ||
inorder_map, 0, inorder.size() - 1); | ||
} | ||
|
||
static auto preorder(TreeNode *node) -> void { | ||
std::function<void(TreeNode*)> print = | ||
[&] (TreeNode *node) -> void { | ||
if (node == nullptr) return; | ||
std::cout << node->value << ' '; | ||
print(node->left); | ||
print(node->right); | ||
}; | ||
|
||
print(node); | ||
} | ||
|
||
private: | ||
static auto helper(const std::vector<int32_t> &preorder, const int32_t pre_start, | ||
const std::size_t pre_end, std::unordered_map<int32_t, int32_t> &inorder_map, | ||
const int32_t in_start, const std::size_t in_end) -> TreeNode* { | ||
if (pre_start > pre_end || in_start > in_end) return nullptr; | ||
|
||
const int32_t root_val = preorder[pre_start]; | ||
auto *root = new TreeNode(root_val); | ||
|
||
const int32_t in_root_index = inorder_map[root_val]; | ||
const int32_t left_subtree_size = in_root_index - in_start; | ||
|
||
root->left = helper(preorder, pre_start + 1, | ||
pre_start + left_subtree_size, | ||
inorder_map, in_start, in_root_index - 1); | ||
root->right = helper(preorder, pre_start + left_subtree_size + 1, | ||
pre_end, inorder_map, | ||
in_root_index + 1, in_end); | ||
|
||
return root; | ||
} | ||
}; |
10 changes: 10 additions & 0 deletions
10
Medium/ConstructBinaryTreeFromPreorderAndInorderTraversal/tests/test.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,10 @@ | ||
#include "../include/solution.hpp" | ||
|
||
auto main() -> int { | ||
std::vector<int32_t> preorder = {3, 9, 20, 15, 7}; | ||
std::vector<int32_t> inorder = {9, 3, 15, 20, 7}; | ||
|
||
Solution::preorder(Solution::build_tree(preorder, inorder)); | ||
|
||
return 0; | ||
} |