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 3349 HASH
题目链接:http://poj.org/problem?id=3349 题意:你可能听说话世界上没有两片相同的雪花,我们定义一个雪花有6个瓣,如果存在有2个雪花相同[雪花是环形的,所以相同可以是旋转过 ...
- iOS10 UI教程视图调试
iOS10 UI教程视图调试 iOS10 UI教程视图调试,当视图很复杂的时候,层次结构就不会很简单了.Xcode可以通过视图(View)调试帮助开发者解决层次结构复杂的问题.视图调试是在Xcode ...
- 短信猫 TIdTCPServer TIdTCPClient
短信猫 服务端: IdTCPServer1: TIdTCPServer; IdAntiFreeze1: TIdAntiFreeze; unit UnitSever; interface uses Wi ...
- 三进制状压 HDOJ 3001 Travelling
题目传送门 题意:从某个点出发,所有点都走过且最多走两次,问最小花费 分析:数据量这么小应该是状压题,旅行商TSP的变形.dp[st][i]表示状态st,在i点时的最小花费,用三进制状压.以后任意进制 ...
- Android自动化测试 - Robotium之Robotium在不同分辨率下clickonview不支持解决方案
使用Robotium中的clickonview方法进行点击操作时,可能在你本机上能够顺利执行,但把脚本移植到不同分辨率的设备下却有可能点不到控件的情况. 网上找了一些资料,基本一条语句可以搞定: 在m ...
- 关于Ue4的深度模板
http://www.unrealchina.net/forum.php?mod=viewthread&tid=100234
- 424 - Integer Inquiry
Integer Inquiry One of the first users of BIT's new supercomputer was Chip Diller. He extended his ...
- topcoder SRM 625 DIV2 IncrementingSequence
由于题目数据量比较小,故可以开辟一个数组存储每个index出现的次数 然后遍历即可 string canItBeDone(int k, vector<int> A){ vector< ...
- ACM 盗梦空间
盗梦空间 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 <盗梦空间>是一部精彩的影片,在这部电影里,Cobb等人可以进入梦境之中,梦境里的时间会比现实中 ...
- 51Nod 1136 欧拉函数 Label:数论
对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等.例如:φ(8) = 4(Phi( ...