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 ...
随机推荐
- JQuery学习之操作CSS
样式表: .important{ font-weight:bold; font-size:xx-large; } .blue{ color:blue; } 1.addClass():向被选元素添加一个 ...
- Codeforces Round #355 (Div. 2)-C
C. Vanya and Label 题目链接:http://codeforces.com/contest/677/problem/C While walking down the street Va ...
- 使用recon/domains-hosts/baidu_site模块,枚举baidu网站的子域
使用recon/domains-hosts/baidu_site模块,枚举baidu网站的子域 [实例3-1]使用recon/domains-hosts/baidu_site模块,枚举baidu网站的 ...
- [技术学习]js正则表达式汇总
一.常用正则表达式关键字 ".":任意字符 "*":任意个数 "+":任意个数,至少一个 "?":0-1个 " ...
- C# 词法分析器(二)输入缓冲和代码定位
系列导航 (一)词法分析介绍 (二)输入缓冲和代码定位 (三)正则表达式 (四)构造 NFA (五)转换 DFA (六)构造词法分析器 (七)总结 一.输入缓冲 在介绍如何进行词法分析之前,先来说说一 ...
- DHL 快递跟踪查询
思路描述:主要使用正则表达式解析. 返回一个跟踪步骤列表. public class TrackingData { public string time { get; se ...
- three.js正交投影照相机
照相机又分为正交投影照相机与透视投影照相机 举个简单的例子来说明正交投影与透视投影照相机的区别.使用透视投影照相机获得的结果是类似人眼在真实世界中看到的有"近大远小"的效果(如下图 ...
- Sqlserver自定义函数Function
一.FUNCTION: 在sqlserver2008中有3中自定义函数:标量函数/内联表值函数/多语句表值函数,首先总结下他们语法的异同点: 同点:1.创建定义是一样的: ...
- ora-00031:session marked for kill处理oracle中杀不掉的锁
http://www.cnblogs.com/songdavid/articles/2223869.html 一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定 ...
- CF#335 Board Game
Board Game time limit per test 2.5 seconds memory limit per test 256 megabytes input standard input ...