-
public TreeNode InPostCreat(int[] In, int[] Post, int lin, int hin, int lpost, int hpost) {
-
TreeNode root = new TreeNode(0);
-
if (Post.length == 0 || In.length == 0) {
-
return null;
-
}
-
root.val = Post[hpost];
-
int i;
-
for (i = lin; In[i] != Post[hpost]; i++)
-
;// 得到中序遍历数组的根节点位置
-
-
int lleng = i - lin;
-
int rleng = hin - i;
-
-
if (lleng > 0) {
-
root.leftchild = InPostCreat(In, Post, lin, lin + lleng - 1, lpost, lpost + lleng - 1);
-
} else {
-
root.leftchild = null;
-
}
-
if (rleng > 0) {
-
root.rightchild = InPostCreat(In, Post, hin - rleng + 1, hin, hpost - rleng, hpost - 1);
-
} else {
-
root.rightchild = null;
-
}
-
return root;
-
}
|