Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is an example to illustrate the problem.

BinaryTree

Input: Two arrays that represent Inorder and level order traversals of a Binary Tree
in[]    = {4, 8, 10, 12, 14, 20, 22};
level[] = {20, 8, 22, 4, 12, 10, 14};

Output: Construct the tree represented by the two arrays. For the above two arrays, the constructed tree is shown in the diagram.

geeksforgeeks的做法是,每次以in和level数组去构建以level[0]为根结点的树。生成下一次level结点的开销是O(n),所以整个时间复杂度是O(n^2)。

我的做法是:

1. 先计算出所有点的层序号。基于这个规律,如果两个元素在同一层,那么后面的数在中序遍历的顺序中,必然也是处于后面;如果后面的数在中序遍历中处于前面,那么必然是处于下一层。O(n)可以做到,但是需要先对两个数组作索引。

2. 从最后一层开始,每一层的左结点,是在inorder序列中,在它左边的连续序列(该序列必须保证层数比它大)中第一个层数=它的层数+1的数。右结点同理。查找左右结点的开销需要O(n)。

所以最终可以做到$O(n^2)$。

  1. struct TreeNode {
  2. int val;
  3. TreeNode *left, *right;
  4. TreeNode(int v): val(v), left(NULL), right(NULL) {}
  5. };
  6.  
  7. void print(TreeNode *root) {
  8. if (root == NULL) {
  9. cout << "NULL ";
  10. } else {
  11. cout << root->val << " ";
  12. print(root->left);
  13. print(root->right);
  14. }
  15. }
  16.  
  17. struct Indices {
  18. int inOrderIndex;
  19. int levelOrderIndex;
  20. int level;
  21. };
  22.  
  23. int main(int argc, char** argv) {
  24. vector<int> inOrder = {, , , , , , };
  25. vector<int> levelOrder = {, , , , , , };
  26.  
  27. // build indices
  28. unordered_map<int, Indices> indices;
  29. for (int i = ; i < inOrder.size(); ++i) {
  30. if (indices.count(inOrder[i]) <= ) {
  31. indices[inOrder[i]] = {i, , };
  32. } else {
  33. indices[inOrder[i]].inOrderIndex = i;
  34. }
  35. if (indices.count(levelOrder[i]) <= ) {
  36. indices[levelOrder[i]] = {, i, };
  37. } else {
  38. indices[levelOrder[i]].levelOrderIndex = i;
  39. }
  40. }
  41.  
  42. // get level no. for each number
  43. int level = ;
  44. for (int i = ; i < levelOrder.size(); ++i) {
  45. if (indices[levelOrder[i]].inOrderIndex < indices[levelOrder[i - ]].inOrderIndex) {
  46. ++level;
  47. }
  48. indices[levelOrder[i]].level = level;
  49. }
  50.  
  51. unordered_map<int, TreeNode*> nodes;
  52. for (int i = levelOrder.size() - ; i >= ; --i) {
  53. nodes[levelOrder[i]] = new TreeNode(levelOrder[i]);
  54. int index = indices[levelOrder[i]].inOrderIndex;
  55. for (int j = index - ; j >= && indices[inOrder[j]].level > indices[inOrder[index]].level; --j) {
  56. if (indices[inOrder[j]].level == indices[inOrder[index]].level + ) {
  57. nodes[levelOrder[i]]->left = nodes[inOrder[j]];
  58. break;
  59. }
  60. }
  61. for (int j = index + ; j < levelOrder.size() && indices[inOrder[j]].level > indices[inOrder[index]].level; ++j) {
  62. if (indices[inOrder[j]].level == indices[inOrder[index]].level + ) {
  63. nodes[levelOrder[i]]->right = nodes[inOrder[j]];
  64. break;
  65. }
  66. }
  67. }
  68. print(nodes[levelOrder[]]);
  69. cout << endl;
  70. return ;
  71. }

Construct a tree from Inorder and Level order traversals的更多相关文章

  1. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

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

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

  3. Construct Binary Tree from Inorder and Postorder Traversal

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

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

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

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

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

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

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

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

  9. Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal

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

随机推荐

  1. POJ 3349 HASH

    题目链接:http://poj.org/problem?id=3349 题意:你可能听说话世界上没有两片相同的雪花,我们定义一个雪花有6个瓣,如果存在有2个雪花相同[雪花是环形的,所以相同可以是旋转过 ...

  2. iOS10 UI教程视图调试

    iOS10 UI教程视图调试 iOS10 UI教程视图调试,当视图很复杂的时候,层次结构就不会很简单了.Xcode可以通过视图(View)调试帮助开发者解决层次结构复杂的问题.视图调试是在Xcode ...

  3. 短信猫 TIdTCPServer TIdTCPClient

    短信猫 服务端: IdTCPServer1: TIdTCPServer; IdAntiFreeze1: TIdAntiFreeze; unit UnitSever; interface uses Wi ...

  4. 三进制状压 HDOJ 3001 Travelling

    题目传送门 题意:从某个点出发,所有点都走过且最多走两次,问最小花费 分析:数据量这么小应该是状压题,旅行商TSP的变形.dp[st][i]表示状态st,在i点时的最小花费,用三进制状压.以后任意进制 ...

  5. Android自动化测试 - Robotium之Robotium在不同分辨率下clickonview不支持解决方案

    使用Robotium中的clickonview方法进行点击操作时,可能在你本机上能够顺利执行,但把脚本移植到不同分辨率的设备下却有可能点不到控件的情况. 网上找了一些资料,基本一条语句可以搞定: 在m ...

  6. 关于Ue4的深度模板

    http://www.unrealchina.net/forum.php?mod=viewthread&tid=100234

  7. 424 - Integer Inquiry

     Integer Inquiry  One of the first users of BIT's new supercomputer was Chip Diller. He extended his ...

  8. topcoder SRM 625 DIV2 IncrementingSequence

    由于题目数据量比较小,故可以开辟一个数组存储每个index出现的次数 然后遍历即可 string canItBeDone(int k, vector<int> A){ vector< ...

  9. ACM 盗梦空间

    盗梦空间 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 <盗梦空间>是一部精彩的影片,在这部电影里,Cobb等人可以进入梦境之中,梦境里的时间会比现实中 ...

  10. 51Nod 1136 欧拉函数 Label:数论

    对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等.例如:φ(8) = 4(Phi( ...