Construct a tree from Inorder and Level order traversals
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的更多相关文章
- 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 ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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 ...
- 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 ...
- 【题解二连发】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 ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 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 ...
随机推荐
- HDU 4858 项目管理 分块
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4858 题解: 下面说一个插入查询时间复杂度为sqrt(m)的算法: 对每个点定义两个值:val,su ...
- c#中ref和out 关键字
问题:为什么c#中要有ref和out?(而java中没有)需求假设:现需要通过一个叫Swap的方法交换a,b两个变量的值.交换前a=1,b=2,断言:交换后a=2,b=1. 现编码如下: class ...
- 使用maven 如何生成源代码的jar包
http://hw1287789687.iteye.com/blog/1943157
- Leap Motion发布新平台,直击下一代移动端VR/AR手部追踪
2013年,动作捕捉技术公司Leap Motion发布了面向PC的体感控制器,不过销量并不乐观.随着2014年虚拟现实技术的再一次兴起,它发布一款用于Oculus Rift的附加设备,从而正式登上VR ...
- 【CodeVS】1993草地排水
题目描述 Description 在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水 ...
- BZOJ4546: codechef XRQRS
Description 给定一个初始时为空的整数序列(元素由1开始标号)以及一些询问: 类型1:在数组后面就加入数字x. 类型2:在区间L…R中找到y,最大化(x xor y). 类型3:删除数组最后 ...
- Maven的第一个小程序
这里是介绍关于maven的第一个小程序 关于maven的安装 : Install Maven in your computer 先看看目录结构: 这是本来的项目目录结构,由于maven有自己的目录结构 ...
- XCode编译文件过多导致内存吃紧解决方法
XCode编译文件过多导致内存吃紧解决方法 /Users/~~/Library/Developer/Xcode/DerivedData 1) 然后 找到编译文件 删除 就好了哦 快去试试看吧
- tomcat、Linux服务器
tomcat.Linux服务器 用到的命令 解压命令: tar -zxvf 文件名 配置 : vi /etc/profile 按 i 进入 ...
- select..in(参数化) 解决注入式问题
方案1 为where in的每一个参数生成一个参数,写法上比较麻烦些,传输的参数个数有限制,最多2100个,可以根据需要使用此方案 using (SqlConnection conn = new Sq ...