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

分析:后序序列的最后一个元素就是树根,然后在中序序列中找到这个元素(由于题目保证没有相同的元素,因此可以唯一找到),中序序列中这个元素的左边就是左子树的中序,右边就是右子树的中序,然后根据刚才中序序列中左右子树的元素个数可以在后序序列中找到左右子树的后序序列,然后递归的求解即可

  1. /**
  2. * Definition for binary tree
  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. typedef vector<int>::iterator Iter;
  13. TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
  14. // IMPORTANT: Please reset any member data you declared, as
  15. // the same Solution instance will be reused for each test case.
  16. return buildTreeRecur(inorder.begin(), inorder.end(), postorder.begin(), postorder.end());
  17. }
  18. TreeNode *buildTreeRecur(Iter istart, Iter iend, Iter pstart, Iter pend)
  19. {
  20. if(istart == iend)return NULL;
  21. int rootval = *(pend-);
  22. Iter iterroot = find(istart, iend, rootval);
  23. TreeNode *res = new TreeNode(rootval);
  24. res->left = buildTreeRecur(istart, iterroot, pstart, pstart+(iterroot-istart));
  25. res->right = buildTreeRecur(iterroot+, iend, pstart+(iterroot-istart), pend-);
           return res;
  26. }
  27. };

LeetCode: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. /**
  2. * Definition for binary tree
  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. typedef vector<int>::iterator Iter;
  13. TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
  14. // IMPORTANT: Please reset any member data you declared, as
  15. // the same Solution instance will be reused for each test case.
  16. return buildTreeRecur(inorder.begin(), inorder.end(), preorder.begin(), preorder.end());
  17. }
  18. TreeNode *buildTreeRecur(Iter istart, Iter iend, Iter pstart, Iter pend)
  19. {
  20. if(istart == iend)return NULL;
  21. int rootval = *pstart;
  22. Iter iterroot = find(istart, iend, rootval);
  23. TreeNode *res = new TreeNode(rootval);
  24. res->left = buildTreeRecur(istart, iterroot, pstart+, pstart++(iterroot-istart));
  25. res->right = buildTreeRecur(iterroot+, iend, pstart++(iterroot-istart), pend);
  26. return res;
  27. }
  28. };

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3440560.html

LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

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

    Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...

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

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

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

  4. leetcode-1006 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-21]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 ...

  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】105 & 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 ...

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

  9. 【一天一道LeetCode】#106. Construct Binary Tree from Inorder and Postorder Traversall

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

随机推荐

  1. (第五章)java面向对象之this的作用总结

    this关键字总是指向调用该方法的对象. this可以代表任何对象,当this出现在某个方法体中时,它所代表的对象是不确定的,但它的类型是确定的,它所代表的对象只能是当前类的(在那个类中就是那个类), ...

  2. javascript 的默认对象

    一.日期对象 格式 :   日期对象名称=new Date([日期参数]) 日期参数: 1.省略(最常用)                                      2.英文-参数格式 ...

  3. ISO9126软件质量模型

    ISO9126软件质量模型,是评价软件质量的国际标准.6个特性27个子特性组成. ISO/IEC9126软件质量模型是一种评价软件质量的通用模型,包括3个层次: 1.质量特性 2.质量子特性 3.度量 ...

  4. CSRF 攻击原理和防御方法

    1. CSRF攻击原理 CSRF(Cross site request forgery),即跨站请求伪造.我们知道XSS是跨站脚本攻击,就是在用户的浏览器中执行攻击者的脚本,来获得其cookie等信息 ...

  5. 读书笔记——Windows环境下32位汇编语言程序设计(3)求复数模的子程序

    3.6.1.1中的例子 _Calc proc _dwX,_dwY local @dwResult finit fild _dwX fld st(0) fmul ;i*i fild _dwY fld s ...

  6. js获取页面传过来的参数

    //接收页面传过来的值 //RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i" ...

  7. 微信公众平台开发教程--方培工作室,PHP语言版本

    准备工作 微信公众平台的注册 介绍如何注册一个微信公众账号. 入门教程 微信公众平台开发入门教程 内容:1.申请SAE作为服务器; 2.启用开发模式; 3.微信公众平台PHP SDK; 4.接收发送消 ...

  8. Mac OS X 设置取消开机自动启动

    1. 启动系统设置 (System Preferences) 2. 点以上截图的 用户/组 (User&Groups) 3. 切换到 “登录选项” (Login Items) 可以看到有saf ...

  9. oracle11G在linux环境下的卸载操作

    1.使用SQL*PLUS停止数据库[oracle@OracleTest oracle]$ sqlplus logSQL> connect / as sysdbaSQL> shutdown ...

  10. C++ enum

    为啥需要枚举类型 编程语言中的所有特性都是为了满足某种需求,达到某个目的还出现.不会莫名其妙的出现在那. 枚举可以用来保存一组属性的值.enum的全称是enumeration意思是列举 看着这句话可能 ...