1、



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 duplicates do not exist in the tree.

代码:

  1. class Solution {
  2. public:
  3. TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
  4. TreeNode* root = NULL;
  5. int len1 = inorder.size();
  6. int len2 = postorder.size();
  7. if(len1<1 || len2<1 || len1!=len2){
  8. return root;
  9. }
  10. return buildTreeCore(inorder,0,len1-1,postorder,0,len2-1);
  11. }
  12. TreeNode* buildTreeCore(vector<int>&inorder, int startIndex1, int endIndex1, vector<int>&postorder, int startIndex2, int endIndex2){
  13. if(endIndex1 < startIndex1 ){
  14. return NULL;
  15. }
  16. TreeNode* root = new TreeNode(postorder[endIndex2]);
  17. int index = 0;
  18. for(int i=startIndex1; i<=endIndex1; ++i){
  19. if(inorder[i] == postorder[endIndex2]){
  20. index = i;
  21. break;
  22. }
  23. }
  24. int leftLen = index-startIndex1;
  25. TreeNode* leftNode = NULL;
  26. TreeNode* rightNode = NULL;
  27.  
  28. leftNode = buildTreeCore(inorder,startIndex1,index-1,postorder,startIndex2,startIndex2+leftLen-1);
  29. rightNode = buildTreeCore(inorder,index+1,endIndex1,postorder,startIndex2+leftLen,endIndex2-1);
  30.  
  31. root->left = leftNode;
  32. root->right = rightNode;
  33. return root;
  34. }
  35. };

2、Construct Binary Tree from Preorder and Inorder Traversal

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

Note:

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

  1. class Solution {
  2. public:
  3. TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
  4. TreeNode* root = NULL;
  5. int len1 = inorder.size();
  6. int len2 = preorder.size();
  7. if(len1<1 || len2<1 || len1!=len2){
  8. return root;
  9. }
  10. return buildTreeCore(inorder,0,len1-1,preorder,0,len2-1);
  11. }
  12. TreeNode* buildTreeCore(vector<int>&inorder, int startIndex1, int endIndex1, vector<int>&preorder, int startIndex2, int endIndex2){
  13. if(endIndex1 < startIndex1 ){
  14. return NULL;
  15. }
  16. TreeNode* root = new TreeNode(preorder[startIndex2]);
  17. int index = 0;
  18. for(int i=startIndex1; i<=endIndex1; ++i){
  19. if(inorder[i] == preorder[startIndex2]){
  20. index = i;
  21. break;
  22. }
  23. }
  24. int leftLen = index-startIndex1;
  25. TreeNode* leftNode = NULL;
  26. TreeNode* rightNode = NULL;
  27.  
  28. leftNode = buildTreeCore(inorder,startIndex1,index-1,preorder,startIndex2+1,startIndex2+leftLen);
  29. rightNode = buildTreeCore(inorder,index+1,endIndex1,preorder,startIndex2+leftLen+1,endIndex2);
  30.  
  31. root->left = leftNode;
  32. root->right = rightNode;
  33. return root;
  34. }
  35. };

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  8. 【题解二连发】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 ...

  9. Construct Binary Tree from Inorder and Postorder Traversal

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

随机推荐

  1. 关于Python的装饰器

    false 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable {m ...

  2. java 获取config 配置文件

    static ResourceBundle PropertiesUtil = ResourceBundle.getBundle("config"); public static S ...

  3. 【BZOJ 1208】[HNOI2004]宠物收养所

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用set搞. (因为规定了不会有相同特点值的东西. 所以可以不用multiset. 那么每次用lower_bound找离它最近的配对 ...

  4. HDU 2222 Keywords Search AC自己主动机入门题

    单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...

  5. oracle 11g RAC手动卸载grid,no deinstall

    1.通过root用户进入到grid的ORACLE_HOME [root@db01]# source /home/grid/.bash_profile [root@db01]# cd $ORACLE_H ...

  6. js html 事件冒泡

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  7. 智课雅思短语---五、 in contrast / on the contrary

    智课雅思短语---五. in contrast / on the contrary 一.总结 一句话总结:相反 in contrast / on the contrary. 1.replace/ su ...

  8. 1.MySQL与MongoDB的操作对比,以及区别

    转自:https://www.cnblogs.com/chris-oil/p/4982490.html MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoD ...

  9. django session深入

    转至原文  https://www.cnblogs.com/zhaof/p/6281468.html 基于cookie做用户验证时:敏感信息不适合放在cookie中 session依赖cookie s ...

  10. [COI2007] [luogu P1823] Patrik 音乐会的等待 解题报告 (单调栈)

    题目链接:https://www.luogu.org/problemnew/show/P1823 题目: N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人. ...