题目:

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
 
递归思路:
  1. 根据后序遍历的特点,知道后序遍历最后一个节点为根节点,即为A
  2. 观察中序遍历,A左侧CBEDF为A左子树节点,A后侧HGJI为A右子树节点
  3. 然后递归的构建A的左子树和后子树

实现:

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return creatTree(inorder.begin(),inorder.end(),postorder.begin(),postorder.end());
}
private:
template<typename InputIterator>
TreeNode *creatTree(InputIterator in_beg,InputIterator in_end,InputIterator post_beg,InputIterator post_end)
{
if(in_beg==in_end||post_beg==post_end) return nullptr; //空树
TreeNode *root=new TreeNode(*(post_end-));
auto inRootPos=find(in_beg,in_end,root->val);//中序遍历中找到根节点,返回迭代指针
int leftlen=distance(in_beg,inRootPos);//中序遍历起点指针与找到的根节点指针的距离
root->left=creatTree(in_beg,inRootPos,post_beg,next(post_beg,leftlen));//递归构建左子数
root->right=creatTree(next(inRootPos),in_end,next(post_beg,leftlen),post_end-);//递归构建右子树
return root;
}
};

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)的更多相关文章

  1. [LeetCode] 106. 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 ...

  2. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  3. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  4. Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...

  5. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

  6. (二叉树 递归) leetcode 106. 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 ...

  7. [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 tha ...

  8. LeetCode 106. 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 ...

  9. 【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 ...

  10. C#解leetcode 106. 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 ...

随机推荐

  1. BZOJ1180 [CROATIAN2009]OTOCI 【LCT】

    题目 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通.如果是则输出"no".否则输出&qu ...

  2. 《c程序设计语言》-2.9

    #include <stdio.h> /*int bitcount(unsigned x) { int b; for(b = 0;x != 0;x >>= 1) { if(x ...

  3. 原生js获取屏幕的宽高

    function client(){ if(window.innerHeight !== undefined){ return { "width": window.innerWid ...

  4. CodeWarrior的map文件详解

    前言 map文件保存了你的整个程序编译链接后的各种信息,包括编译器链接器信息,内存分配信息,对象依赖等,每次编译链接程序后,这个文件都会被覆盖重新生成. 对我来说,它最主要的作用是它详尽的描述了整个程 ...

  5. openGL初学函数解释汇总

    openGL初学函数解释汇总 1.GLUT工具包提供的函数 //GLUT工具包所提供的函数 glutInit(&argc, argv);//对GLUT进行初始化,这个函数必须在其它的GLUT使 ...

  6. Android APK瘦身方法小结

    众所周知,APP包体的大小,会影响推广的难度,用户不太喜欢下载太大的APP,同类型同等功能的APP中往往是包体小的更受用户的青睐,所以降低包体是一项非常必要的事情,也是最近公司的APP需要降低包体,所 ...

  7. jquery判断ie与使用ie来判断ie,推荐ie样式块

    用jquery来判断浏览器类型,如果只是仅仅为了判断浏览器的类型而使用该方法,那么不建议使用,只是在你已经使用了jquery才建议使用,因为没必要因为这么小的一个功能就加载那么大的类库吧 Jquery ...

  8. vue-cli打包后图片路径取不到的问题

    今天准备把vue-cli build 的文件发到服务器上单发现会出现图片找不到的问题 解决办法如下 修改 assetsPublicPath: './' .打开webpack.prod.conf.js, ...

  9. web应用性能优化经验总结

    常见性能优化要求      在我经历的性能优化案例中,常见的问题都是这样开始的:      a) 前台访问很慢,请帮忙分析优化      b) 用户对性能很不满意,再不解决就要投诉      c) 数 ...

  10. 5.DataFrame(基本概念)