根据一棵树的中序遍历与后序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
递归
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {number[]} inorder
* @param {number[]} postorder
* @return {TreeNode}
*/
var buildTree = function (inorder, postorder) {
let i = (p = postorder.length - 1);
const dfs = (stop) => {
if (inorder[i] !== stop) {
const root = new TreeNode(postorder[p--]);
root.right = dfs(root.val);
i--;
root.left = dfs(stop);
return root;
}
return null;
};
return dfs();
};
博客介绍根据一棵树的中序遍历与后序遍历构造二叉树,且假设树中无重复元素,采用递归方法实现。

389

被折叠的 条评论
为什么被折叠?



