# 先中根构造有相同元素的二叉树

0

public static <T> BinaryTree<T> BinaryTreeCreate(T[] prelist, T[] inlist) {
BinaryNode<T> root = BinaryTreeCreate(0, inlist.length - 1, 0, prelist, inlist);
return new BinaryTree<>(root);
}
public static <T> BinaryNode<T> BinaryTreeCreate(int left, int right, int pos, T[] prelist, T[] inlist) {
int len = right - left + 1;
T croot = prelist[pos];
BinaryNode<T> node = new BinaryNode<>(croot);
if (len <= 1) {
return node;
}
int mid = Index(inlist, croot, left, right);
if (mid != left) {
node.left = BinaryTreeCreate(left, mid - 1, pos + 1, prelist, inlist);
}
if (mid != right) {
int rpos = pos + mid - left + 1;
node.right = BinaryTreeCreate(mid + 1, right, rpos, prelist, inlist);
}
return node;
}
private static <T> int Index(T[] data, T target, int left, int right) {
for (int j = left; j <= right; j++) {
if (data[j].equals(target)) {
return j;
}
}
return -1;
}
Integer[] PRE_SEQ = { 1, 2, 6, 7, 8, 5, 3, 6, 7, 8 };
Integer[] MID_SEQ = {7, 6, 8, 2, 5, 1, 3, 7, 6, 8 };这个可以构造

Integer[] MID_SEQ = {7, 6, 8, 2, 5, 6, 3, 7, 6, 8 };构造不了

mmjkly | 初学一级 | 园豆：102

0

您需要登录以后才能回答，未注册用户请先注册