添加0106.从中序与后序遍历序列构造二叉树 JavaScript版

This commit is contained in:
faiz-lab
2021-05-14 15:28:50 +08:00
parent fea16ee053
commit a9fb92fc11

View File

@ -623,7 +623,21 @@ Python
Go 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) * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)