-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path106.从中序与后序遍历序列构造二叉树.cpp
100 lines (97 loc) · 2.5 KB
/
106.从中序与后序遍历序列构造二叉树.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
/*
* @lc app=leetcode.cn id=106 lang=cpp
*
* [106] 从中序与后序遍历序列构造二叉树
*/
#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> &inorder, vector<int> &postorder)
{
return build(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1);
}
TreeNode *build(vector<int> &inorder, int inStart, int inEnd, vector<int> &postorder, int postStart, int postEnd)
{
if (inStart > inEnd)
return NULL;
auto root = new TreeNode(postorder[postEnd]);
// 从中序遍历找到根
int idx = inStart;
for (; idx <= inEnd; idx++)
{
if (inorder[idx] == postorder[postEnd])
{
break;
}
}
int leftSize = idx - inStart;
// 左子树
root->left = build(inorder, inStart, idx - 1, postorder, postStart, postStart + leftSize - 1);
root->right = build(inorder, idx + 1, inEnd, postorder, postStart + leftSize, postEnd - 1);
return root;
}
};
// @lc code=end
int main()
{
Solution s;
vector<int> pre = {9, 3, 15, 20, 7};
vector<int> in = {9, 15, 7, 20, 3};
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 << "]";
}