Question:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Anwser 1 :
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *createTree(vector<int> &inorder, int inBeg, int inEnd, vector<int> &postorder, int postBeg, int postEnd)
{
if (inBeg > inEnd) return NULL;
int root = postorder[postEnd];
int index;
for(int i = inBeg; i <= inEnd; i++)
if (inorder[i] == root)
{
index = i;
break;
}
int len = index - inBeg;
TreeNode *left = createTree(inorder, inBeg, index - 1, postorder, postBeg, postBeg + len - 1);
TreeNode *right = createTree(inorder, index + 1, inEnd, postorder, postBeg + len, postEnd - 1);
TreeNode *node = new TreeNode(root);
node->left = left;
node->right = right;
return node;
}
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (inorder.size() == 0) return NULL;
return createTree(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1);
}
};
Anwser 2 :
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
std::unordered_map<int,int> index_map;
void buildMap(vector<int> &inorder) {
index_map.clear();
for (int i = 0; i < inorder.size(); i++) {
index_map[inorder[i]] = i;
}
}
TreeNode *buildTreeInOrder(vector<int> &postorder, int post_offset, vector<int> &inorder, int in_offset, int len)
{
if (!len) return NULL;
TreeNode *root = new TreeNode(postorder[post_offset]);
int i = index_map[postorder[post_offset]];
root->right = buildTreeInOrder(postorder, post_offset-1, inorder, in_offset, in_offset-i);
root->left = buildTreeInOrder(postorder, post_offset-(in_offset-i)-1, inorder, in_offset-(in_offset-i)-1, len-(in_offset-i)-1);
return root;
}
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (postorder.size() == 0 && inorder.size() == 0) {
return NULL;
}
buildMap(inorder);
return buildTreeInOrder(postorder, postorder.size()-1, inorder, inorder.size()-1, postorder.size());
}
};
参考推荐:
Construct Binary Tree from Inorder and Postorder Traversal
分享到:
相关推荐
Javascript 的解决方案Leetcode Problems and interview problems in Javascript10 Regular Expresion Matching.js100 Same Tree.js101 Symmetric Tree.js102 Binary Tree Level Order Traversal.js103 Binary Tree ...
106.construct-binary-tree-from-inorder-and-postorder-traversal (从中序与后序遍历序列构造二叉树) 112.path-sum (路径总和) 116.populating-next-right-pointers-in-each-node (填充每个节点的下一个右侧节点
lru缓存leetcode LeetCode_Note leetcode 个人笔记 ...[106_construct-binary-tree-from-inorder-and-postorder-traversal.cpp] [107_binary-tree-level-order-traversal-ii.cpp] [108_convert-sorted
leetcode 跳跃 Algorithm 算法题解: 包括书籍算法, 程序员算法面试指南, 还有leetcode算法题 ...construct-binary-tree-from-inorder-and-postorder-traversal 无官方题解 116 populating-next-right-pointers-in-eac
421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...