根据前序和中序遍历重建二叉树
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function reConstructBinaryTree(pre, vin)
{
// write code here
if(pre.length == 0 || vin.length ==0) return null
var root = new TreeNode(pre[0])
var index = vin.indexOf(pre[0])
root.left = reConstructBinaryTree(pre.slice(1,index+1),vin.slice(0,index))
root.right = reConstructBinaryTree(pre.slice(index+1),vin.slice(index+1))
return root
}树算法 文章被收录于专栏
树相关算法
