地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
in_left 和 in_right 都是相对中序vector invec而言的,从invec的in_left到in_right位置创建节点。
当既有左子树又有右子树时,左子树的根节点位置即为当前根节点位置pre_pos加上1. 右子树的根节点位置即为根节点当前位置pre_pos + 左子树长度( i - in_left ) + 1 .
参考代码:
vector<int>prevec, invec;
TreeNode* Construct(int pre_pos, int in_left, int in_right)
{
if(pre_pos>=prevec.size() || in_left>in_right)
{
return NULL;
}
TreeNode* p = new TreeNode(prevec[pre_pos]);
if(in_left==in_right)
return p;
int i = 0;
for(; i < invec.size(); ++i)
{
if(prevec[pre_pos]==invec[i])
break;
}
//left tree null
if(i == in_left)
{
p->right = Construct(pre_pos+1, in_left+1, in_right);
}
//right tree null
else if(i==in_right)
{
p->left = Construct(pre_pos+1, in_left, in_right-1);
}
else
{
p->left = Construct(pre_pos+1, in_left, i-1);
p->right = Construct(pre_pos+i-in_left+1, i+1, in_right);
}
return p;
}
class Solution {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
if(preorder.empty())
return NULL;
prevec = preorder;
invec = inorder;
return Construct(0, 0, preorder.size()-1);
}
};
//SECOND TRIALclass Solution {private :TreeNode * construct ( int pre [], int in [], int len ){if ( len <= 0 )return NULL ;int root_val = pre [ 0 ], offset = 0 ;TreeNode * root = new TreeNode ( root_val );while ( in [ offset ] != root_val )++ offset ;root -> left = construct ( pre + 1 , in , offset );root -> right = construct ( pre + offset + 1 , in + offset + 1 , len - offset - 1 );return root ;}public :TreeNode * buildTree ( vector < int > & preorder , vector < int > & inorder ) {int * pre = & preorder [ 0 ], * in = & inorder [ 0 ];return construct ( pre , in , preorder . size ());}};