-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstruct_binary_tree_from_preorder.cpp
More file actions
81 lines (71 loc) · 2.1 KB
/
construct_binary_tree_from_preorder.cpp
File metadata and controls
81 lines (71 loc) · 2.1 KB
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
// =====================================================================================
//
// Filename: construct_binary_tree_from_order.cpp
//
// Description: 105. Construct Binary Tree from Preorder and Inorder Traversal.
//
// Version: 1.0
// Created: 08/07/2019 08:48:02 PM
// Revision: none
// Compiler: g++
//
// Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
// Organization:
//
// =====================================================================================
#include <stdio.h>
#include <vector>
using std::vector;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution
{
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder)
{
if (preorder.size() != inorder.size() || preorder.size() < 1)
{
return nullptr;
}
return buildTree(preorder, inorder, 0, 0, preorder.size());
}
private:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder,
int pre_start, int in_start, int sub_size)
{
if (sub_size <= 0)
{
return nullptr;
}
int i = 0;
for (i = in_start; i < (in_start + sub_size); i++)
{
if (preorder[pre_start] == inorder[i])
{
// Found
break;
}
}
if (i >= (in_start + sub_size))
{
return nullptr;
}
TreeNode* root = new TreeNode(preorder[pre_start]);
root->left = buildTree(preorder, inorder, pre_start + 1, in_start, i - in_start);
root->right = buildTree(preorder, inorder, pre_start + (i - in_start + 1), i + 1, sub_size - (i - in_start + 1));
return root;
}
};
int main(int argc, char* argv[])
{
vector<int> preorder = {3, 9, 20, 15, 7};
vector<int> inorder = {9, 3, 15, 20, 7};
auto* root = Solution().buildTree(preorder, inorder);
printf("root != nullptr -> %s\n", ((root != nullptr) ? "true" : "false"));
return 0;
}