题解 | #重建二叉树#
重建二叉树
https://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param preOrder int整型一维数组 * @param vinOrder int整型一维数组 * @return TreeNode类 */ function reConstructBinaryTree( preOrder , vinOrder ) { if(preOrder.length==0||vinOrder.length==0) return null let root =preOrder[0] let index = vinOrder.indexOf(root) let tree = new TreeNode(root) tree.left = reConstructBinaryTree(preOrder.slice(1,index+1),vinOrder.slice(0,index)) tree.right = reConstructBinaryTree(preOrder.slice(index+1), vinOrder.slice(index+1)) return tree } module.exports = { reConstructBinaryTree : reConstructBinaryTree };