import { TreeNode } from './BinaryTree' ; param { number [ ] } preorder param { number [ ] } inorder return { TreeNode } function buildTreeByPreAndIn ( preorder , inorder ) { if ( ! preorder . length && ! inorder . length ) { return null ; } let root = new TreeNode ( preorder [ 0 ] ) ; let position = inorder . indexOf ( root . val ) ; let midLeft = inorder . slice ( 0 , position ) ; let midRight = inorder . slice ( position + 1 ) ; let preLeft = preorder . slice ( 1 , position + 1 ) ; let preRight = preorder . slice ( position + 1 ) ; root . left = buildTreeByPreAndIn ( preLeft , midLeft ) ; root . right = buildTreeByPreAndIn ( preRight , midRight ) ; return root ; } param { number [ ] } postOrder param { number [ ] } inOrder return { TreeNode } function buildTreeByPostAndIn ( postOrder , inOrder ) { if ( ! postOrder . length && ! inOrder . length ) { return null ; } let root = new TreeNode ( postOrder [ postOrder . length - 1 ] ) ; let position = inOrder . indexOf ( root . val ) ; let midLeft = inOrder . slice ( 0 , position ) ; let midRight = inOrder . slice ( position + 1 ) ; let postLeft = postOrder . slice ( 0 , position ) ; let postRight = postOrder . slice ( position , postOrder . length - 1 ) ; root . left = buildTreeByPostAndIn ( postLeft , midLeft ) ; root . right = buildTreeByPostAndIn ( postRight , midRight ) ; return root ; } export { buildTreeByPreAndIn , buildTreeByPostAndIn } ;