-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConstructBinaryTree.cpp
143 lines (120 loc) · 3.36 KB
/
ConstructBinaryTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Construct Binary Tree from Preorder and Inorder Traversal
// https://leetcode.com/explore/interview/card/top-interview-questions-medium/108/trees-and-graphs/788/
#include <iostream>
#include <unordered_map>
#include <vector>
struct Node
{
public:
int key;
Node* left;
Node* right;
Node() : key{0}, left{nullptr}, right{right} { }
Node(int k) : key{k}, left{nullptr}, right{nullptr} { }
};
class Tree
{
private:
Node* root;
void destroyTree(Node* node);
void inorder(Node* node, std::vector<int>& traversal);
void preorder(Node* node, std::vector<int>& traversal);
Node* constructTree(std::vector<int>& inorder, std::vector<int>& preorder,
int start, int end, int& index, std::unordered_map<int,int>& indexMap);
public:
Tree();
~Tree();
std::vector<int> inorder();
std::vector<int> preorder();
void constructTree(std::vector<int>& inorder, std::vector<int>& preorder);
};
Tree::Tree() : root{nullptr} { }
Tree::~Tree()
{
if (root == nullptr) {
return;
}
destroyTree(root);
}
void Tree::destroyTree(Node* node)
{
if (node != nullptr) {
destroyTree(node->left);
destroyTree(node->right);
delete node;
}
}
void Tree::inorder(Node* node, std::vector<int>& traversal)
{
if (node == nullptr) {
return;
}
inorder(node->left, traversal);
traversal.push_back(node->key);
inorder(node->right, traversal);
}
void Tree::preorder(Node* node, std::vector<int>& traversal)
{
if (node == nullptr) {
return;
}
traversal.push_back(node->key);
preorder(node->left, traversal);
preorder(node->right, traversal);
}
std::vector<int> Tree::inorder()
{
std::vector<int> traversal;
inorder(root, traversal);
return traversal;
}
std::vector<int> Tree::preorder()
{
std::vector<int> traversal;
preorder(root, traversal);
return traversal;
}
Node* Tree::constructTree(std::vector<int>& inorder, std::vector<int>& preorder,
int start, int end, int& index, std::unordered_map<int,int>& indexMap)
{
if (start > end) {
return nullptr;
}
Node* node = new Node(preorder[index++]);
int iIndex = indexMap[node->key];
node->left = constructTree(inorder, preorder, start, iIndex - 1, index, indexMap);
node->right = constructTree(inorder, preorder, iIndex + 1, end, index, indexMap);
return node;
}
void Tree::constructTree(std::vector<int>& inorder, std::vector<int>& preorder)
{
std::unordered_map<int,int> indexMap;
for (int i = 0; i < inorder.size(); ++i) {
indexMap[inorder[i]] = i;
}
int index = 0;
root = constructTree(inorder, preorder, 0, static_cast<int>(inorder.size()) - 1, index, indexMap);
}
void printVector(std::vector<int>& vec)
{
for (auto i : vec) {
std::cout << i << " ";
}
std::cout << "\n";
}
int main()
{
std::vector<int> preorder {3,9,20,15,7};
std::vector<int> inorder {9,3,15,20,7};
Tree tree;
tree.constructTree(inorder, preorder);
std::cout << "================================\n";
std::vector<int> preTraversal = tree.preorder();
std::cout << "Preorder traversal : ";
printVector(preTraversal);
std::cout << "================================\n";
std::vector<int> inTraversal = tree.inorder();
std::cout << "Inorder traversal : ";
printVector(inTraversal);
return 0;
}