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)$。

 struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int v): val(v), left(NULL), right(NULL) {}
}; void print(TreeNode *root) {
if (root == NULL) {
cout << "NULL ";
} else {
cout << root->val << " ";
print(root->left);
print(root->right);
}
} struct Indices {
int inOrderIndex;
int levelOrderIndex;
int level;
}; int main(int argc, char** argv) {
vector<int> inOrder = {, , , , , , };
vector<int> levelOrder = {, , , , , , }; // build indices
unordered_map<int, Indices> indices;
for (int i = ; i < inOrder.size(); ++i) {
if (indices.count(inOrder[i]) <= ) {
indices[inOrder[i]] = {i, , };
} else {
indices[inOrder[i]].inOrderIndex = i;
}
if (indices.count(levelOrder[i]) <= ) {
indices[levelOrder[i]] = {, i, };
} else {
indices[levelOrder[i]].levelOrderIndex = i;
}
} // get level no. for each number
int level = ;
for (int i = ; i < levelOrder.size(); ++i) {
if (indices[levelOrder[i]].inOrderIndex < indices[levelOrder[i - ]].inOrderIndex) {
++level;
}
indices[levelOrder[i]].level = level;
} unordered_map<int, TreeNode*> nodes;
for (int i = levelOrder.size() - ; i >= ; --i) {
nodes[levelOrder[i]] = new TreeNode(levelOrder[i]);
int index = indices[levelOrder[i]].inOrderIndex;
for (int j = index - ; j >= && indices[inOrder[j]].level > indices[inOrder[index]].level; --j) {
if (indices[inOrder[j]].level == indices[inOrder[index]].level + ) {
nodes[levelOrder[i]]->left = nodes[inOrder[j]];
break;
}
}
for (int j = index + ; j < levelOrder.size() && indices[inOrder[j]].level > indices[inOrder[index]].level; ++j) {
if (indices[inOrder[j]].level == indices[inOrder[index]].level + ) {
nodes[levelOrder[i]]->right = nodes[inOrder[j]];
break;
}
}
}
print(nodes[levelOrder[]]);
cout << endl;
return ;
}

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 1743 后缀数组

    题目链接:http://poj.org/problem?id=1743 题意:给定一个钢琴的音普序列[值的范围是(1~88)],现在要求找到一个子序列满足 1,长度至少为5 2,序列可以转调,即存在两 ...

  2. POJ 2418 字典树

    题目链接:http://poj.org/problem?id=2418 题意:给定一堆树的名字,现在问你每一棵树[无重复]的出现的百分比,并按树名的字典序输出 思路:最简单的就是用map来写,关于字典 ...

  3. A - The Moronic Cowmpouter

    Description Inexperienced in the digital arts, the cows tried to build a calculating engine (yes, it ...

  4. Python profiling

    Profiling(性能调试)是我一直很感兴趣的一个话题,之前给大家介绍过Datadog这个工具,今天我们来看看Python语言中有哪些方法来做Profiling. Poorman's Profile ...

  5. ie不支持getElementsByName的解决办法

    在chrome下getElementsByName运行正常,可在IETester7~11下都不支持  w3c规范中getElementsByName是按着name属性进行检索的,而MS的IE却是按着i ...

  6. javaEE基础

    1.拦截器与过滤器 过滤器(filter),过滤器处于客户端与Web资源(Servlet.JSP.HTML)之间,客户端与Web资源之间的请求和响应都要通过过滤器进行过滤.如过滤编码,IP 拦截器(i ...

  7. Repeater 双向排序

      做项目的时候,DataGrid ,DataList,Repeater 三个控件都是很优秀的数据显示控件,DataGrid的方便,简单易用,功能强大,但对性能会有所影响,在loading页面的时候大 ...

  8. 如何查看项目svn路径

    1.选择项目根目录---->鼠标右键---->属性---->版本控制(Subversion) 如图:

  9. Extjs tree的相关方法及配置项

    Ext.tree.TreePanel       主要配置项:             root:树的根节点.             rootVisible:是否显示根节点,默认为true.     ...

  10. CentOS6.4 配置iptables

    如果没有安装iptables可以直接用yum安装 yum install -t iptables 检查iptables服务的状态, service iptables status 如果出现“iptab ...