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. (转)qsort和sort

    1.qsort函数: 原 型: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)) ...

  2. event 内存泄漏

    组长说用event有内存泄漏的隐患..做个测试. 预留

  3. http://blog.csdn.net/chenleixing/article/details/43740759

    http://blog.csdn.net/chenleixing/article/details/43740759

  4. HBase Shell 常见操作

    1.一般操作 status 查看状态 version 查看HBase版本 2.DDL操作 create 'member','member_id','address','info' 创建了一个membe ...

  5. Target:IG

    https://www.zhihu.com/question/25525630 别人轻轻松松红名,我拼死挣扎才1700+分. 仔细想想,虽然我在这东西上花了太多的精力,可是我根本没有认真学.做题全靠抄 ...

  6. UVa 11082 & 最大流的行列模型

    题意: 给出一个矩阵前i行的和与前j列的和,(i∈[1,r],j属于[1,c]),每个元素ai,j∈[1,20],请你还原出这个矩阵,保证有解. SOL: 给网络流建模跪了,神一样的建图,如果我我会怎 ...

  7. CUDA程序设计(三)

    算法设计:基数排序 CUDA程序里应当尽量避免递归,因而在迭代排序算法里,基数排序通常作为首选. 1.1 串行算法实现 十进制位的基数排序需要考虑数位对齐问题,比较麻烦.通常实现的是二进制位的基数排序 ...

  8. bzoj3083 遥远的国度 题解

    题目大意: 给定一棵有根树,每个点有一个权值,提供三种操作: 1.将x节点变为根节点 2.将x到y路径上的点的权值全部改为v 3.询问x的子树中点权的最小值 思路: 用DFS序剖分,记录每个节点入栈出 ...

  9. Human and AI's future (reverie)

    However, I do notice that to make the dark situation happen, it doesn't require the topleft matrix t ...

  10. CentOS 命令【备忘】

    1.查看物理cpu个数 grep 'physical id' /proc/cpuinfo | sort -u | wc -l 2.查看核心数量 grep 'core id' /proc/cpuinfo ...