题目

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:

You may assume that duplicates do not exist in the tree.

Show Tags

Show Similar Problems

分析

跟上一道题同样的道理。

AC代码

  1. class Solution {
  2. public:
  3. template <typename Iter>
  4. TreeNode* make(Iter in_begin, Iter in_end , Iter post_begin, Iter post_end ) {
  5. if (post_begin == post_end || in_begin == in_end)
  6. return NULL;
  7. int size = post_end - post_begin;
  8. //后序遍历最后一个节点为树的根节点
  9. TreeNode *root = new TreeNode(*(post_begin + size-1));
  10. //在中序遍历结果中查找根节点
  11. Iter iter = find(in_begin, in_end, *(post_begin + size - 1));
  12. //计算左子树个数
  13. int count = iter - in_begin;
  14. if (iter != in_end)
  15. {
  16. //则在inOrder中(0 , count-1)为左子树中序遍历结果(count+1,size-1)为右子树的中序遍历序列
  17. //在preOrder中(0,count-1)为左子树前序遍历结果(count,size-2)为右子树前序遍历结果
  18. root->left = make(in_begin, iter , post_begin, post_begin + count);
  19. //构造右子树
  20. root->right = make(iter + 1, in_end ,post_begin + count, post_begin + size - 1);
  21. }
  22. return root;
  23. }
  24. TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
  25. if (inorder.empty() || postorder.empty())
  26. return NULL;
  27. return make(inorder.begin(), inorder.end() ,postorder.begin(), postorder.end());
  28. }
  29. };

GitHub测试程序源码

LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

  1. 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...

  2. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  3. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  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:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  6. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

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

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

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

  9. 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...

随机推荐

  1. php—常见设计模式

    工厂模式 /** * 工厂方法或者类生成对象,而不是在代码中直接new * * 修改类名的时候,不需要每一个实例化语句都修改 * 只需要修改对应的工厂方法 * * Class Factory * @p ...

  2. centos 6.x下pxe+tftp+http+kickstart无人值守安装操作系统

    1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过 ...

  3. HDU 1028 Ignatius and the Princess III dp整数划分

    http://acm.hdu.edu.cn/showproblem.php?pid=1028 dp[i][j]表示数值为i,然后最小拆分的那个数是j的时候的总和. 1 = 1 2 = 1 + 1 . ...

  4. qq登录,新浪微博登录接口申请过程中遇到的问题

    接口申请下来了,开发很容易的,参数传到就可以了.以前就做过这方面的开发,但是申请还是第一次,网上有关这方面的东东不是很多,所以记录一下申请过程. 1,qq登录接口申请 申请地址是:http://con ...

  5. vue2.0:(四)、首页入门,组件拆分1

    为什么需要组件拆分呢?这样才能更符合模块化这样一个理念. 首先是index.html,代码如下: <!DOCTYPE html> <html> <head> < ...

  6. vue+element ui项目总结点(四)零散细节概念巩固如vue父组件调用子组件的方法、拷贝数据、数组置空问题 等

    vue config下面的index.js配置host: '0.0.0.0',共享ip (假设你的电脑启动了这个服务我电脑一样可以启动)-------------------------------- ...

  7. Ubuntu下Postgres安装与配置

    postgres8.4安装配置:1.安装postgres8.4~$ sudo apt-get install postgresql 2.修改超级管理员postgres密码:以系统用户运行psql~$ ...

  8. asp.net 页面嵌套(非iframe)方法

    前台 <div id="divUrlDetail" runat="server"> </div> 后台 protected void P ...

  9. ImportError: No module named flask.ext.wtf 解决方法

    install pip install flask.ext.wtf

  10. 与调试器共舞 - LLDB 的华尔兹

    你是否曾经苦恼于理解你的代码,而去尝试打印一个变量的值? 1 NSLog(@"%@", whatIsInsideThisThing); 或者跳过一个函数调用来简化程序的行为? 1 ...