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 ...
随机推荐
- POJ 1743 后缀数组
题目链接:http://poj.org/problem?id=1743 题意:给定一个钢琴的音普序列[值的范围是(1~88)],现在要求找到一个子序列满足 1,长度至少为5 2,序列可以转调,即存在两 ...
- SQL初级第三课(上)
先建立一个表 create table Student --学生(Sno char(3) primary key , --学生学号Sname ...
- 【转】】Android ADB命令大全
ADB很强大,记住一些ADB命令有助于提高工作效率. 获取序列号: adb get-serialno 查看连接计算机的设备: adb devices 重启机器: adb reboot 重启到bootl ...
- JS运动基础
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- HDU4758 Walk Through Squares(AC自动机+状压DP)
题目大概说有个n×m的格子,有两种走法,每种走法都是一个包含D或R的序列,D表示向下走R表示向右走.问从左上角走到右下角的走法有多少种走法包含那两种走法. D要走n次,R要走m次,容易想到用AC自动机 ...
- python程序设计语言笔记 第一部分 程序设计基础
1.1.1中央处理器(CPU) cpu是计算机的大脑,它从内存中获取指令然后执行这些指令,CPU通常由控制单元和逻辑单元组成. 控制单元用来控制和协调除cpu之外的其他组件的动作. 算数单元用来完成数 ...
- (转)MySQL提示“too many connections”的解决办法
link:http://www.cfp8.com/mysql-prompt-too-many-connections-solution.html 今天生产服务器上的MySQL出现了一个不算太陌生的错误 ...
- Android --SeekBar的使用
1. 效果图
- 【Eclipse】修改 编码格式
eclipse 默认编码居然是GBK,js文件默认编码是ISO-....怎么可以这样呢? 都修改成UTF8的方法: 1.windows->Preferences...打开"首选项&qu ...
- tableFooterView中的按钮点击没反应
一,经历 1.查了按钮没有响应的几个方法,排除了是用户交互设置为 NO 的情况. 2.然后打印了一下tableFooterView,尽然发现其高度为0,而且我也没有设置 frame, 却可以显示按钮, ...