-
Notifications
You must be signed in to change notification settings - Fork 1
/
105.从前序与中序遍历序列构造二叉树.cpp
108 lines (104 loc) · 2.7 KB
/
105.从前序与中序遍历序列构造二叉树.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
/*
* @lc app=leetcode.cn id=105 lang=cpp
*
* [105] 从前序与中序遍历序列构造二叉树
*/
#include <iostream>
#include <vector>
#include <queue>
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) {}
};
// @lc code=start
/**
* Definition for a binary tree node.
* 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:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder)
{
return build(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
}
TreeNode *build(vector<int> &preorder, int preStart, int preEnd, vector<int> &inorder, int inStart, int inEnd)
{
if (preStart > preEnd)
return NULL;
// if (inStart > inEnd)
// return NULL;
// if (preStart > preorder.size() - 1)
// return NULL;
auto rootVal = preorder[preStart];
// 找到根
auto root = new TreeNode(rootVal);
// 找到左子树
int inIdx = inStart;
for (; inIdx <= inEnd; inIdx++)
{
if (inorder[inIdx] == rootVal)
{
break;
}
}
int leftSize = inIdx - inStart;
// 递归左子树
root->left = build(preorder, preStart + 1, preStart + leftSize, inorder, inStart, inIdx - 1);
// 递归右子树
root->right = build(preorder, preStart + leftSize + 1, preEnd, inorder, inIdx + 1, preEnd);
return root;
}
};
// @lc code=end
int main()
{
Solution s;
vector<int> pre = {3, 9, 20, 15, 7};
vector<int> in = {9, 3, 15, 20, 7};
TreeNode *root = s.buildTree(pre, in);
// 层序遍历
queue<TreeNode *> q;
q.push(root);
// auto a = new TreeNode(9);
// cout << a->val;
// cout << a->val;
cout << '[';
while (!q.empty())
{
TreeNode *now = q.front();
cout << now->val;
q.pop();
if (now->left != nullptr)
{
q.push(now->left);
}
else
{
cout << "null";
}
if (now->right != nullptr)
{
q.push(now->right);
}
else
{
cout << "null";
}
cout << ",";
}
cout << "]";
}