Question:
Given preorder and inorder 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 *build(vector<int> &preorder, vector<int> &inorder, int sPre, int ePre, int sMid, int eMid) {
if (sPre > ePre || sMid > eMid) return NULL;
int pivot = preorder[sPre];
int pos;
for (pos = sMid; pos <= eMid; pos++) {
if (inorder[pos] == pivot)
break;
}
TreeNode *parent = new TreeNode(pivot);
parent->left = build(preorder, inorder, sPre + 1, sPre + (pos - sMid), sMid, pos - 1);
parent->right = build(preorder, inorder, sPre + (pos - sMid) + 1, ePre, pos + 1, eMid);
return parent;
}
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return build(preorder, inorder, 0, preorder.size() - 1, 0, inorder.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:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int size = preorder.size();
if(size == 0) return NULL;
return BuildBT(preorder, inorder, 0, 0, size-1);
}
TreeNode *BuildBT(vector<int> &preorder, vector<int> &inorder, int pos, int start, int end){
if(start > end ) return NULL;
int j = std::find(inorder.begin() + start, inorder.end() + end, preorder[pos]) - inorder.begin();
TreeNode *root = new TreeNode(inorder[j]);
root->left = BuildBT(preorder, inorder, pos+1, start, j-1);
root->right = BuildBT(preorder, inorder, j+pos-start+1, j+1, end); // j+pos-start+1 is the relative position in the preorder array of the next right child
return root;
}
};
分享到:
相关推荐
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题解之144_Binary_Tree_Preorder_Traversal
javascript js_leetcode题解之144-binary-tree-preorder-traversal.js
python python_leetcode题解之106_Construct_Binary_Tree_from_Inorder
js js_leetcode题解之106-construct-binary-tree-from-inorder
94.Binary_Tree_Inorder_Traversal二叉树的中序遍历【LeetCode单题讲解系列】
java java_leetcode-107-binary-tree-level-order-traversal
java java_leetcode-102-binary-tree-level-order-traversal
java java_leetcode-105-construct-binary-tree-from-preorder-and-inorde
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
144.Binary_Tree_Preorder_Traversal二叉树的前序遍历【LeetCode单题讲解系列】
js js_leetcode题解之107-binary-tree-level-order-traversal-ii.js
java java_leetcode题解之Construct String from Binary Tree.java