From a9fb92fc113bbd09d8e53691b3cf179f4c8f4605 Mon Sep 17 00:00:00 2001 From: faiz-lab Date: Fri, 14 May 2021 15:28:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A00106.=E4=BB=8E=E4=B8=AD?= =?UTF-8?q?=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91=20JavaSc?= =?UTF-8?q?ript=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...中序与后序遍历序列构造二叉树.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index e51cf08b..4cc2a88a 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -623,7 +623,21 @@ Python: Go: - +JavaScript +```javascript +var buildTree = function(inorder, postorder) { + if (!postorder.length) return null + + let root = new TreeNode(postorder[postorder.length - 1]) + + let index = inorder.findIndex(number => number === root.val) + + root.left = buildTree(inorder.slice(0, index), postorder.slice(0, index)) + root.right = buildTree(inorder.slice(index + 1, inorder.length), postorder.slice(index, postorder.length - 1)) + + return root +}; +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)