leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal

这道题是为数不多的感觉在读本科的时候见过的问题。人工构造的过程是怎样呢,后续遍历最后一个节点一定是整棵树的根节点,从中序遍历中查找到这个元素,就可以把树分为两颗子树,这个元素左侧的递归构造左子树,右侧的递归构造右子树,元素本身分配空间,作为根节点。

于set和map容器不同的是,vector容器不含find的成员函数,应该用stl的库函数,好在返回的也是迭代器,而vector的迭代器之间是可以做减法的,偏移量很方便的得到。

TreeNode *buildRec(vector<int> &inorder, int si, vector<int> &postorder, int so, int len){
    if(len <= 0)    return NULL;
    TreeNode *root = new TreeNode(postorder[so]);
    int index = find(inorder.begin(), inorder.end(), postorder[so]) - inorder.begin();
    int newlen = index - si;
    root->left = buildRec(inorder, si, postorder, so-len+newlen, newlen);
    root->right = buildRec(inorder, index+1, postorder, so-1, len-newlen-1);
    return root;
}

class Solution {
public:
    TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
        return buildRec(inorder, 0, postorder, inorder.size()-1, inorder.size());
    }
};

leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal

时间: 2024-08-29 14:12:36

leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal的相关文章

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

leetcode笔记:Construct Binary Tree from Inorder and Postorder Traversal

一. 题目描述 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 二. 题目分析 这道题和Construct Binary Tree from Preorder and Inorder Traversal类似,都是考察基本概念的,后序遍历是先遍历左子树,然后遍历右子树,最

leetcode || 106、Construct Binary Tree from Inorder and Postorder Traversal

problem: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Hide Tags Tree Array Depth-first Search 题意:给定二叉树的中序遍历序列和后续遍历序列,构建这棵二叉树,也很经典 thinking: (1)这种类型的题,要举个例子找

Leetcode 随笔之 ------ Construct Binary Tree from Inorder and Postorder Traversal

利用一棵二叉树的中序遍历的结果数组和后续遍历的结果数组复原该树: 采用分治策略,解析如下图: 如图:中序遍历数组的division特征为左(0 --> x) 根(x + 1) 右(x + 2 --> length - 1) 后序遍历数组的division特征为左(0 --> x) 根(x + 1 --> length - 2) 右(length - 1) 我们可以将中序和后序的数组的左部分和右部分作为根节点的左子树和右子树进行递归处理 那么如何在这两个数组中找到划分division

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

leetcode第一刷_Balanced Binary Tree

二叉平衡树好火啊,几乎每个公司的笔试题里都有它,考了好多次我都不会,挂笔试很有可能就是因为它,还有一个它的同伙叫二叉搜索树,貌似人气比它还要高一些.二叉平衡树是什么样的树呢,是每个节点的左右子树高度相差绝对值都不超过1.好,你说你终于回了,这不很简单吗,求一下根节点的左右字数高度,如果满足,他就是,否则就不是嘛.不是啊亲,要求是所有节点都满足这个条件,判断的时候必须每个节点都验证的! 扯了这么长,其实看看代码就明白了,怎么有种在贴吧发言要凑够15字的感觉. int getHeight(TreeN

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 60461 Total Submissions: 203546 Difficulty: Medium Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not

[leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序遍历为{4,5,2,6,7,3,1},我们可以反推回去.由于后序遍历的最后一个节点就是树的根.也就是roo