这道题是为数不多的感觉在读本科的时候见过的问题。

人工构造的过程是如何呢。兴许遍历最后一个节点一定是整棵树的根节点。从中序遍历中查找到这个元素,就能够把树分为两颗子树,这个元素左側的递归构造左子树,右側的递归构造右子树。元素本身分配空间,作为根节点。

于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());}
};

转载于:https://www.cnblogs.com/blfshiye/p/5135053.html

leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal相关推荐

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

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

  2. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal

    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal Solution1:我的答案 仿照105题写的答案 / ...

  3. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal Construct Binary Tree f

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

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

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

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

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

  9. Construct Binary Tree from Inorder and Postorder Traversal

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

最新文章

  1. python中文字符画、编写程序合理选取中文字符构造_编写程序,合理选取中文字符构造字符表,生成中文字符画。...
  2. 面试官:哪些场景会产生OOM?怎么解决?
  3. Java Web系列:Spring MVC基础
  4. python_面向对象进阶之slots
  5. Linux的cmake3的安装 cmake3编译安装成功了的 yum对于cmake3表示成功但实际没成功
  6. 未能加载包studio package_Xrepo:一个现代化的跨平台 C/C++ 包管理器
  7. python 删除重复字符_Google面试问题指南:使用Python删除重复出现的字符
  8. 计算机的微程序存放在dram,计算机组成与结构
  9. linux 自动获取ip和dns,linux 配置IP和DNS
  10. java递归生成树结构_突破CRUD | 万能树Java工具类封装(源码)
  11. 对zebra的一点思考 --- 002
  12. 如何选择VC界面库产品?(二)– DSkinLite界面库介绍
  13. 学习Spring之前要先学习什么?
  14. 韩立刚老师《计算机网络》笔记3
  15. 基统计的方法对进行多因子的探索性数据分析
  16. POI word 内容提取 Strict OOXML isn‘t currently supported, please see bug #57699
  17. 禁止查看网页源文件的代码
  18. LibJpeg的安装与修复颜色错误图像错位保姆级教程
  19. 浏览器输入url后经历的过程(详细)
  20. git命令和遇见得 warning:

热门文章

  1. 部署IPV6有什么好处?
  2. vs中imshow函数报错_Win7下VS2010中配置Opencv2.4.4的方法(32位和64位都有效)(亲测成功)...
  3. python结束线程池正在运行的线程_python之线程与线程池
  4. STP文件服务器,综合监控stp服务器
  5. tmp ubuntu 自动删除吗_Linux tmp目录自动清理总结
  6. vue是什么_什么是VUE?vue有什么作用?
  7. 您不是订单管理的定向开发者_Web Summit 2020大会:华为在欧洲发布HMS Connect,持续助力合作伙伴与开发者的创新增长...
  8. 图像“位操作”有什么用?
  9. 分类模型的评估方法-正确率(Accuracy)
  10. Python几个简单好用的基础功能