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

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

题意:根据中序遍历和后序遍历,构建二叉树

思路很清晰,做法很简单,就不讲了。

一开始我写了一个递归的解法,本地测试数据都OK,无奈提交的时候内存超出限制,下面先给出超出内存的代码:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
  13. if(inorder.size()==)
  14. return nullptr;
  15. if(inorder.size()==)
  16. return new TreeNode(inorder[]);
  17. int fath=postorder[postorder.size()-];
  18. TreeNode* root=new TreeNode(fath);
  19. int flag=-;
  20. for(int i=;i<inorder.size();i++)
  21. {
  22. if(inorder[i]==fath)
  23. {
  24. flag=i;
  25. break;
  26. }
  27. }
  28. vector<int> left;
  29. for(int i=;i<flag;i++)
  30. left.push_back(inorder[i]);
  31. vector<int> right;
  32. for(int i=flag+;i<inorder.size();i++)
  33. right.push_back(inorder[i]);
  34. int flag1=-;
  35. for(int i=;i<postorder.size();i++)
  36. {
  37. if(postorder[i]==inorder[flag])
  38. {
  39. flag1=i;
  40. break;
  41. }
  42. }
  43.  
  44. vector<int> left1;
  45. for(int i=;i<flag1;i++)
  46. left1.push_back(postorder[i]);
  47. vector<int> right1;
  48. for(int i=flag1;i<postorder.size()-;i++)
  49. right1.push_back(postorder[i]);
  50.  
  51. root->left=buildTree(left,left1);
  52. root->right=buildTree(right,right1);
  53. return root;
  54. }
  55. };

有没有看出问题,没错,就是第28、31、44、47行的代码,每次递归都会产生新的vector数组,所以最后导致内存超出限制。所以改进了一下,新定义一个方法helper来递归,helper里面的实现不再申请新的vector空间,直接在参数inorder和postorder中进行操作,从而避免内存超出限制。下面是accepted的代码:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
  13. return helper(inorder,,inorder.size()-,postorder,,postorder.size()-);
  14. }
  15.  
  16. TreeNode* helper(vector<int>& inorder,int begin1,int end1,vector<int>& postorder,int begin2,int end2)
  17. {
  18. if(begin1>end1)
  19. return nullptr;
  20. if(begin1==end1)
  21. return new TreeNode(inorder[begin1]);
  22.  
  23. TreeNode* root=new TreeNode(postorder[end2]);
  24. int i=begin1;
  25. for(;i<=end1;i++)
  26. {
  27. if(inorder[i]==postorder[end2])
  28. break;
  29. }
  30. int leftlen=i-begin1;
  31.  
  32. root->left=helper(inorder,begin1,begin1+leftlen-,postorder,begin2,begin2+leftlen-);
  33. root->right=helper(inorder,begin1+leftlen+,end1,postorder,begin2+leftlen,end2-);
  34. return root;
  35. }
  36. };

leetcode-1006 Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

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

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

  2. 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: ...

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

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

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

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

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

  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. leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal

    代码实现:给定一个中序遍历和后序遍历怎么构造出这颗树!(假定树中没有重复的数字) 因为没有规定是左小右大的树,所以我们随意画一颗数,来进行判断应该是满足题意的. 3 / \ 2 4 /\ / \1 6 ...

随机推荐

  1. tableview的reloadData应注意

    http://blog.csdn.net/ouyangtianhan/article/details/7835041 http://stackoverflow.com/questions/160715 ...

  2. 在线文档转换API word,excel,ppt等在线文件转pdf、png

    在线文档转换API提供word,excel,ppt等在线文件转pdf.png等,文档:https://www.juhe.cn/docs/api/id/259 接口地址:http://v.juhe.cn ...

  3. Android线程之基本用法

    一: 在android中有两种实现线程thread的方法: 一种是,扩展java.lang.Thread类 另一种是,实现Runnable接口 二: Thread类代表线程类,它的两个最主要的方法是: ...

  4. iOS开发——浅谈构架与用户体验

    工作不是千篇一律的重复,从中寻找乐趣才是我们应该做的. 作为一名码农,做过几个项目,每次做项目的时候都会自己构思,如果完全是我自己设计,会怎么去设计?心里一直没有满意的答案,不管怎么布局,好像都感觉差 ...

  5. ios 计算字符串长度<转>

    - (int)textLength:(NSString *)text//计算字符串长度 {     float number = 0.0;     for (int index = 0; index ...

  6. 【转】国外程序员整理的 C++ 资源大全

    内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包括了STL容器,算法和函数等. C++ Standard Library:是一系列类 ...

  7. 集群下Cookie共享,必须要设置machineKey

    这个节允许你设置用于加密数据和创建数字签名的服务器特定的密钥.ASP.NET自动使用它来保护表单验证Cookie,你也可以将它用于受保护的视图状态数据.同时,这个密钥还用于验证进程外的会话状态提供程序 ...

  8. [Angular Tutorial] 8 - Templating Links & Images

    在这一步中,我们将会在电话列表中为电话添加略图,并附上链接,当然现在也不会链接去哪.在随后的步骤中,我们将使用这些链接来展示电话列表中额外的信息. ·现在电话列表中会有链接和图片. 最重要的不同在下面 ...

  9. java--反射和注解

    一.java.lang.reflect类   Class类 1.反射机制(Reflection):通过类创建对象, 2.反射机制提供了如下功能: 在运行时,判断任意一个对象所属的类 构造任意一个类的对 ...

  10. Python使用Selenium/PhantomJS

    安装selenium: 1 pip install selenium 安装PhantomJS: 1 2 3 4 https://bitbucket.org/ariya/phantomjs/downlo ...