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)