Skip to content

Latest commit

 

History

History
17 lines (14 loc) · 678 Bytes

105-已知前中序,求原有的二叉树.md

File metadata and controls

17 lines (14 loc) · 678 Bytes

105-已知前中序,求原有的二叉树

原题

解法

来源

const buildTree = (preorder, inorder) => {
  if (inorder.length == 0) return null;
  const root = new TreeNode(preorder[0]);
  const mid = inorder.indexOf(preorder[0]);
  root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid));
  root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1));
  return root;
};