一.题目

Construct Binary Tree from Preorder and Inorder Traversal

Total Accepted: 36475 Total Submissions: 138308My
Submissions

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

Note:

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

Show Tags
Have you met this question in a real interview?

Yes
No

Discuss












二.解题技巧

     这道题仅仅是考察先序和中序遍历的概念,先序是先訪问根节点,然后訪问左子树。最后訪问右子树;中序遍历是先遍历左子树,然后訪问根节点。最后訪问右子树。

   
做法都是先依据先序遍历的概念,找到先序遍历的第一个值,即为根节点的值。然后依据根节点将中序遍历的结果分成左子树和右子树。然后就能够递归的实现了。

    上述做法的时间复杂度为O(n^2)。空间复杂度为O(1)


三.实现代码

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. /**
  6. * Definition for a binary tree node.
  7. * struct TreeNode {
  8. * int val;
  9. * TreeNode *left;
  10. * TreeNode *right;
  11. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  12. * };
  13. */
  14.  
  15. using std::vector;
  16. using std::find;
  17.  
  18. struct TreeNode
  19. {
  20. int val;
  21. TreeNode *left;
  22. TreeNode *right;
  23. TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  24. };
  25.  
  26. class Solution
  27. {
  28. private:
  29. TreeNode* buildTree(vector<int>::iterator PreBegin, vector<int>::iterator PreEnd,
  30. vector<int>::iterator InBegin, vector<int>::iterator InEnd)
  31. {
  32. if (PreBegin == PreEnd)
  33. {
  34. return NULL;
  35. }
  36.  
  37. int HeadValue = *PreBegin;
  38. TreeNode *HeadNode = new TreeNode(HeadValue);
  39.  
  40. vector<int>::iterator LeftEnd = find(InBegin, InEnd, HeadValue);
  41. if (LeftEnd != InEnd)
  42. {
  43. HeadNode->left = buildTree(PreBegin + 1, PreBegin + (LeftEnd - InBegin) + 1,
  44. InBegin, LeftEnd);
  45. }
  46.  
  47. HeadNode->right = buildTree(PreBegin + (LeftEnd - InBegin) + 1, PreEnd,
  48. LeftEnd + 1, InEnd);
  49.  
  50. return HeadNode;
  51. }
  52. public:
  53. TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder)
  54. {
  55. if (preorder.empty())
  56. {
  57. return NULL;
  58. }
  59.  
  60. return buildTree(preorder.begin(), preorder.end(), inorder.begin(),
  61. inorder.end());
  62.  
  63. }
  64. };



四.体会

    这道题是考察基础概念的题。并不须要非常多算法,仅仅是一个递归的过程。



版权全部,欢迎转载,转载请注明出处,谢谢





LeetCode_Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

  1. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

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

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

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

  5. LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  6. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

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

  8. [LeetCode] 105. 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 ...

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

随机推荐

  1. IT人都欠自已一个Lable Page

    平时, 你是不是喜欢将一些工作或者技术的网站都会Mark一下.慢慢,自已收藏的标签越来越多.收藏后去查找的比较麻烦,而且不是很方便. 下面的文章是介绍我自已是怎么实现一个简单的Lable Page. ...

  2. WPF 让普通 CLR 属性支持 XAML 绑定(非依赖属性),这样 MarkupExtension 中定义的属性也能使用绑定了

    原文:WPF 让普通 CLR 属性支持 XAML 绑定(非依赖属性),这样 MarkupExtension 中定义的属性也能使用绑定了 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4 ...

  3. [Python] for.. not in.. Remove Deduplication

    Write a function, remove_duplicates that takes a list as its argument and returns a new list contain ...

  4. 对string的一些扩展函数

    对string作了一些扩展,包含string转化为int.string转化为double.string转化为bool.打印系统当前时间.但没有解决数据溢出的问题,请大神帮忙解决! //头文件 /*pa ...

  5. modSecurity规则学习(一)——配置文件

    环境:modSecurity3.0,nignx1.13.8 modSecurity配置文件 1.nginx.conf server { listen ; modsecurity on; //启动mod ...

  6. Linux系统存储交换机日志

    Linux系统存储交换机日志     日志记录是为系统设备在运行过程中报告其运行情况而设的, 为了保证系统正常运行, 解决每一天可能遇到的各种各样的问题, 网络管理员必须认真地读取日志记录.目前公司系 ...

  7. js实现 导航移入移出效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. AIX 5.3下创建逻辑卷、添加文件系统并挂载

    首先创建逻辑卷smit lv ,这里没多大问题就不细述了. 输入要创建的逻辑卷名.所属卷组.分配多少个LP.创建在哪块磁盘上等,另外还可以设置镜像,默认是只有一份镜像的,即不做mirror. 到此LV ...

  9. canvas:画布

    画布有默认宽度,如果要自己设置宽带要写在属性上 列: <canvas id = "myCanvas" width = "600" height = &qu ...

  10. ZJOI2005沼泽鳄鱼

    矩阵优化dp ** 注意:矩阵乘法没有交换律 ** 思路:类比P2151hh去散步 代码特点在一维的答案矩阵 1.矩阵优化两点间方案数不必赘述 2.注意2,3,4可以办到以他们的lcm为周期,正是因为 ...