最后更新于
// pre 是前序数组
// vin 是中序数组
function reConstruct(pre, vin) {
if(pre.length === 0){
return null;
}
if(pre.length === 1){
return new TreeNode(pre[0]);
}
const value = pre[0];
const index = vin.indexOf(value);
const vinLeft = vin.slice(0,index);
const vinRight = vin.slice(index+1);
const preLeft = pre.slice(1,index+1);
const preRight = pre.slice(index+1);
const node = new TreeNode(value);
node.left = reConstruct(preLeft, vinLeft);
node.right = reConstruct(preRight, vinRight);
return node;
}