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
分享到:
相关推荐
105.construct_binary_tree_from_preorder_and_inorder_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 ...
java java_leetcode题解之Construct Binary Tree from String.java
python python_leetcode题解之094_Binary_Tree_Inorder_Traversal
c语言基础 c语言_leetcode题解之0094_binary_tree_inorder_traversal.zip
javascript js_leetcode题解之94-binary-tree-inorder-traversal.js
python python_leetcode题解之145_Binary_Tree_Postorder_Traversal
94.Binary_Tree_Inorder_Traversal二叉树的中序遍历【LeetCode单题讲解系列】
145.Binary_Tree_Postorder_Traversal二叉树的后序遍历【LeetCode单题讲解系列】
javascript js_leetcode题解之145-binary-tree-postorder-traversal.js
java java_leetcode-107-binary-tree-level-order-traversal
java java_leetcode-102-binary-tree-level-order-traversal
python python_leetcode题解之106_Construct_Binary_Tree_from_Inorder
java java_leetcode题解之Construct String from Binary Tree.java
js js_leetcode题解之106-construct-binary-tree-from-inorder
python python_leetcode题解之102_Binary_Tree_Level_Order_Traversal
python python_leetcode题解之107_Binary_Tree_Level_Order_Traversal_II
js js_leetcode题解之102-binary-tree-level-order-traversal.js
js js_leetcode题解之107-binary-tree-level-order-traversal-ii.js
java java_leetcode-105-construct-binary-tree-from-preorder-and-inorde