[英]Construct Binary Tree from in-order and post-order Traversals
I came across a problem that really interested me, but I am not sure I fully understand how to complete the task at hand: Design an algorithm to construct a binary tree from two n-long sequences, known to be the output of in-order and post-order traversals of the same binary tree. 我遇到了一个让我真正感兴趣的问题,但我不确定我是否完全理解如何完成手头的任务:设计一种算法,以从两个n长序列构造二叉树,已知这是有序输出和相同二叉树的后遍历。
I've managed to complete that much so far. 到目前为止,我已经完成了很多工作。 Below is my (relevant) code so far, however I also would like to be able to identify sequences for which no binary tree exists.
以下是我到目前为止的代码(相关代码),但是我也希望能够识别不存在二叉树的序列。 I'm not sure how to check for this.
我不确定如何检查。 Can someone give me a nudge in the right direction?
有人可以按正确的方向推动我吗?
node* build_tree(int in[], int inStart, int inEnd,
int post[], int postStart, int postEnd) {
if(inStart > inEnd || postStart > postEnd)
return NULL;
int rootValue = post[postEnd];
node *tNode = new_node(rootValue);
// find the index of this node in in-order traversal
int inIndex = search(in, inStart, inEnd, rootValue);
// Using index in in-order traversal, construct left and right subtrees
tNode->left = build_tree(in, inStart, inIndex-1, post, postStart, postStart+inIndex-(inStart+1));
tNode->right = build_tree(in, inIndex+1, inEnd, post, postStart + inIndex - inStart, postEnd - 1);
return tNode;
}
// Function to find index of value in arr[start...end]
// The function assumes that value is present in in[]
int search(int arr[], int start, int end, int value) {
int i;
for(i = start; i < end; i++) {
if(arr[i] == value)
return i;
}
return i;
}
// function that allocates a new node with the
// given data and NULL left and right pointers
node* new_node(int data) {
node* n = (node*)malloc(sizeof(node));
n->data = data;
n->left = NULL;
n->right = NULL;
return n;
}
AFAIK a binary tree is not possible for the given sequences when , 在以下情况下,给定序列无法使用二叉树AFAIK:
1) The two sequences are not of same length -- we can check the array length 1)两个序列的长度不同-我们可以检查数组的长度
2) The search of postorder value in inorder sequence fails -- search function should be modified to return negative value when search fails instead of returning i(after the for loop) 2)按顺序搜索后序值失败-搜索功能应修改为在搜索失败时返回负值,而不是返回i(在for循环之后)
3) The given sequences do not represent postorder and inorder of the same tree -- as suggested by Gene traverse the tree and check the sequence 3)给定的序列不代表同一棵树的后序和顺序-如Gene所建议的遍历树并检查序列
My Code for this problem :) 我的代码为这个问题:)
int searchelement( vector<int> &v , int start ,int end,int val)
{
for(int i=start;i<=end;i++)
{
if(v[i]==val)
return i;
}
return -1;
}
TreeNode * abc(vector<int> &postorder, vector<int> &inorder , int start ,int end , int &index)
{
if(start>end)
return NULL;
int i = searchelement(inorder,start,end,postorder[index--]);
TreeNode * temp = new TreeNode(inorder[i]);
if(start==end)
{
return temp;
}
temp->right=abc(postorder,inorder,i+1,end,index);
temp->left =abc(postorder,inorder,start,i-1,index);
return temp;
}
main(){
TreeNode * head =NULL;
int start=0,end=inorder.size()-1,index=0;
head= abc(preorder,inorder,start,end,index);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.