修改二叉树系列19 从中序与后序遍历序列构造二叉树JS版本变量名 preorder->inorder

This commit is contained in:
陈梦洁
2022-04-03 14:03:31 +08:00
parent 5c3ab04b6e
commit e48336238d

View File

@ -790,7 +790,7 @@ func findRootIndex(target int,inorder []int) int{
```javascript ```javascript
var buildTree = function(inorder, postorder) { var buildTree = function(inorder, postorder) {
if (!preorder.length) return null; if (!inorder.length) return null;
const rootVal = postorder.pop(); // 从后序遍历的数组中获取中间节点的值, 即数组最后一个值 const rootVal = postorder.pop(); // 从后序遍历的数组中获取中间节点的值, 即数组最后一个值
let rootIndex = inorder.indexOf(rootVal); // 获取中间节点在中序遍历中的下标 let rootIndex = inorder.indexOf(rootVal); // 获取中间节点在中序遍历中的下标
const root = new TreeNode(rootVal); // 创建中间节点 const root = new TreeNode(rootVal); // 创建中间节点