using
System;
class
GFG
{
public
class
Node
{
public
int
data;
public
Node left, right;
};
public
class
INT
{
public
int
a;
public
INT(
int
a){
this
.a=a;}
}
static
Node newNode(
int
data)
{
Node temp =
new
Node();
temp.data = data;
temp.left = temp.right =
null
;
return
temp;
}
static
void
printInorder(Node node)
{
if
(node ==
null
)
return
;
printInorder(node.left);
Console.Write(
"{0} "
, node.data);
printInorder(node.right);
}
static
Node conBinaryTreeUtil(
int
[]pre,
int
[]preM,
INT preIndex,
int
l,
int
h,
int
size)
{
if
(preIndex.a >= size || l > h)
return
null
;
Node root = newNode(pre[preIndex.a]);
++(preIndex.a);
if
(l == h)
return
root;
int
i;
for
(i = l; i <= h; ++i)
if
(pre[preIndex.a] == preM[i])
break
;
if
(i <= h)
{
root.left = conBinaryTreeUtil (pre, preM,
preIndex, i, h, size);
root.right = conBinaryTreeUtil (pre, preM,
preIndex, l + 1, i - 1, size);
}
return
root;
}
static
void
conBinaryTree(Node root,
int
[]pre,
int
[]preMirror,
int
size)
{
INT preIndex =
new
INT(0);
int
preMIndex = 0;
root = conBinaryTreeUtil(pre,preMirror,
preIndex, 0, size - 1, size);
printInorder(root);
}
public
static
void
Main(String []args)
{
int
[]preOrder = {1,2,4,5,3,6,7};
int
[]preOrderMirror = {1,3,7,6,2,5,4};
int
size = preOrder.Length;
Node root =
new
Node();
conBinaryTree(root,preOrder,preOrderMirror,size);
}
}