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. struct TreeNode {
  2. int val;
  3. TreeNode *left;
  4. TreeNode *right;
  5. TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  6. };
  7. class Solution {
  8. public:
  9. TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
  10. // Start typing your C/C++ solution below
  11. // DO NOT write int main() function
  12. root = NULL;
  13. if(inorder.empty()) return root;
  14. root = new TreeNode();
  15. buildSubTree(inorder,postorder,,inorder.size()-,,postorder.size()-,root);
  16. return root;
  17.  
  18. }
  19. void buildSubTree(vector<int> &inorder, vector<int>&postorder, int inStartPos, int inEndPos, int postStartPos, int postEndPos,TreeNode * currentNode)
  20. {
  21. currentNode->val = postorder[postEndPos]; //后序遍历的最后一个节点是根节点
  22.  
  23. //find root position in inorder vector
  24. int inRootPos;
  25. for(int i = inStartPos; i <= inEndPos; i++)
  26. {
  27. if(inorder[i] == postorder[postEndPos])
  28. {
  29. inRootPos = i;
  30. break;
  31. }
  32. }
  33.  
  34. //right tree: 是中序遍历根节点之后的部分,对应后序遍历根节点前相同长度的部分
  35. int newPostPos = postEndPos - max(inEndPos - inRootPos, );
  36. if(inRootPos<inEndPos)
  37. {
  38. currentNode->right = new TreeNode();
  39. buildSubTree(inorder,postorder,inRootPos+,inEndPos,newPostPos,postEndPos-,currentNode->right);
  40. }
  41.  
  42. //leftTree: 是中序遍历根节点之前的部分,对应后序遍历从头开始相同长度的部分
  43. if(inRootPos>inStartPos)
  44. {
  45. currentNode->left = new TreeNode();
  46. buildSubTree(inorder,postorder,inStartPos,inRootPos-,postStartPos,newPostPos-,currentNode->left);
  47. }
  48. }
  49. private:
  50. TreeNode* root;
  51. };

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

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

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

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

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

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

  4. Construct Binary Tree from Inorder and Postorder Traversal

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

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

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

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

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

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

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

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

随机推荐

  1. CentOS6下源码安装mysql-5.6.25

    1.1.系统环境检查 1)检查系统版本 mkdir -p /server/tools/ cd /server/tools/ cat /etc/redhat-release 2)配置域名解析 vim / ...

  2. 首次运行tensorflow-gpu 1.0 报错 failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED

    发现博客: https://blog.csdn.net/u010752600/article/details/79534910 于是找到解决方法. sudo rm -rf ~/.nv/ 

  3. Linux 进程、线程运行在指定CPU核上

    /******************************************************************************** * Linux 进程.线程运行在指定 ...

  4. Swift学习——A Swift Tour 协议和扩展

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhenyu5211314/article/details/28854395 Protocols an ...

  5. Chrome Developer Tools 中的 Preview 不显示 HTML 的问题

    Chrome Developer Tools 中的 Preview 不显示 HTML 的问题 最近升级到 Chrome V64,发现 Chrome Developer Tools 中的 Preview ...

  6. Jquery中的has、find、filter方法区别

    find方法 find返回的是匹配结果集,作用于后代$(‘li’).find(‘.a’).css(‘background-color’, ‘red’);在li下面查找元素是否有class=a的元素,返 ...

  7. VirtualBox的vdi映像导入遇到的uuid冲突问题 (转)

      virtualbox导入vdi文件时出现下面的问题: 打开hard disk D:\software\GT5.0.0.vdi 失败 Cannot register the hard disk 'D ...

  8. settimeout()在IE8下参数无效问题解决方法

    遇到这个问题,setTimeout(Scroll(),3000); 这种写法在IE8 下 不能够执行,提示参数无效, setTimeout(function(){Scroll()},3000);这种方 ...

  9. mysql-10临时表、复制表

    1.创建临时表 mysql临时表在我们需要保存一些临时数据时非常有用. 临时表只在当前连接可见,当关闭连接时,mysql会自动删除表并释放所有空间. 如果使用客户端创建临时表,只有在管不客户端程序时才 ...

  10. 给iOS开发新手送点福利,简述UISwitch的属性和用法

    UISwitch属性 1. onTintColor 处于on时switch 的颜色     switchImage.onTintColor = [UIColor grayColor]; 2.tintC ...