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

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

========

利用:中序+后序遍历

====

code:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
///
template<typename Iterator>
TreeNode *help_buildTree_ip(Iterator in_first,Iterator in_last,
Iterator post_first,Iterator post_last){
if(in_first == in_last) return nullptr;
if(post_first == post_last) return nullptr; auto val = *prev(post_last);
TreeNode *root = new TreeNode(val); auto in_root_pos = find(in_first,in_last,val);
auto left_size = distance(in_first,in_root_pos);
auto post_left_last = next(post_first,left_size); root->left = help_buildTree_ip(in_first,in_root_pos,post_first,post_left_last);
root->right = help_buildTree_ip(next(in_root_pos),in_last,post_left_last,prev(post_last)); return root;
}
TreeNode* buildTree_ip(vector<int> &inorder,vector<int> &postorder){
return help_buildTree_ip(inorder.begin(),inorder.end(),postorder.begin(),postorder.end());
}
};

最新文章

  1. 【记录】vmware fusion 7 windows 10 unidentified network
  2. 微信H5手指滑动屏蔽微信的默认效果
  3. C语言进行面向对象编程
  4. Apache日志配置详解(rotatelogs LogFormat)
  5. oracle的内置函数
  6. 《使用shell位置变量进行目录文件的备份小脚本》
  7. HDU-4737 A Bit Fun 维护
  8. PHP加解密相关函数
  9. c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行
  10. android sax解析xml 文件 动态加载标题
  11. 简单说明CGI是什么
  12. Hadoop学习笔记二
  13. Android调试工具之ADB
  14. .Net Core微服务系列--开篇
  15. 0412ooday01.txt=============对象和类(上)
  16. 【实验四】[bx]和loop的使用
  17. 575. Distribute Candies
  18. PHP+Ajax实现文件上传功能
  19. JVM深度解析
  20. python购物车作业

热门文章

  1. 几个有用的SAP安全配置的用户参数配置列表
  2. tyvj1022 - 进制转换 ——进制为负数
  3. shamir叠像术 分类: 图像处理 2015-07-08 16:50 17人阅读 评论(1) 收藏
  4. leetcode 146. LRU Cache ----- java
  5. json对象和字符串互相转换
  6. C++编程开发学习的50条建议(转)
  7. Java——日期格式
  8. C++中的单例模式
  9. Awesome Deep Vision
  10. JSP 相关试题(四)