-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path106.从中序与后序遍历序列构造二叉树.cpp
46 lines (40 loc) · 1.47 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
/*
* @lc app=leetcode.cn id=106 lang=cpp
*
* [106] 从中序与后序遍历序列构造二叉树
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
#include <vector>
#include <algorithm>
class Solution {
public:
TreeNode* buildTree(std::vector<int>& inorder, std::vector<int>& postorder) {
if(inorder.size() == 0)
return nullptr;
return buildTree(inorder.begin(), inorder.end(), postorder.begin(), postorder.end());
}
private:
TreeNode* buildTree(std::vector<int>::const_iterator inorderStart, std::vector<int>::const_iterator inorderEnd, std::vector<int>::const_iterator postorderStart, std::vector<int>::const_iterator postorderEnd) const{
if(std::distance(inorderStart, inorderEnd) == 0)
return nullptr;
auto* root = new TreeNode(*(postorderEnd - 1));
if(std::distance(inorderStart, inorderEnd) == 1)
return root;
auto it = std::find(inorderStart, inorderEnd, *(postorderEnd-1));
auto* leftRoot = buildTree(inorderStart, it, postorderStart, postorderStart + std::distance(inorderStart, it));
auto* rightRoot = buildTree(it + 1, inorderEnd, postorderStart + std::distance(inorderStart, it), postorderEnd - 1);
root->left = leftRoot;
root->right = rightRoot;
return root;
}
};
// @lc code=end