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)$。

 1 struct TreeNode {
 2     int val;
 3     TreeNode *left, *right;
 4     TreeNode(int v): val(v), left(NULL), right(NULL) {}
 5 };
 6
 7 void print(TreeNode *root) {
 8     if (root == NULL) {
 9         cout << "NULL ";
10     } else {
11         cout << root->val << " ";
12         print(root->left);
13         print(root->right);
14     }
15 }
16
17 struct Indices {
18     int inOrderIndex;
19     int levelOrderIndex;
20     int level;
21 };
22
23 int main(int argc, char** argv) {
24     vector<int> inOrder = {4, 8, 10, 12, 14, 20, 22};
25     vector<int> levelOrder = {20, 8, 22, 4, 12, 10, 14};
26
27     // build indices
28     unordered_map<int, Indices> indices;
29     for (int i = 0; i < inOrder.size(); ++i) {
30         if (indices.count(inOrder[i]) <= 0) {
31             indices[inOrder[i]] = {i, 0, 0};
32         } else {
33             indices[inOrder[i]].inOrderIndex = i;
34         }
35         if (indices.count(levelOrder[i]) <= 0) {
36             indices[levelOrder[i]] = {0, i, 0};
37         } else {
38             indices[levelOrder[i]].levelOrderIndex = i;
39         }
40     }
41
42     // get level no. for each number
43     int level = 0;
44     for (int i = 1; i < levelOrder.size(); ++i) {
45         if (indices[levelOrder[i]].inOrderIndex < indices[levelOrder[i - 1]].inOrderIndex) {
46             ++level;
47         }
48         indices[levelOrder[i]].level = level;
49     }
50
51     unordered_map<int, TreeNode*> nodes;
52     for (int i = levelOrder.size() - 1; i >= 0; --i) {
53         nodes[levelOrder[i]] = new TreeNode(levelOrder[i]);
54         int index = indices[levelOrder[i]].inOrderIndex;
55         for (int j = index - 1; j >= 0 && indices[inOrder[j]].level > indices[inOrder[index]].level; --j) {
56             if (indices[inOrder[j]].level == indices[inOrder[index]].level + 1) {
57                 nodes[levelOrder[i]]->left = nodes[inOrder[j]];
58                 break;
59             }
60         }
61         for (int j = index + 1; j < levelOrder.size() && indices[inOrder[j]].level > indices[inOrder[index]].level; ++j) {
62             if (indices[inOrder[j]].level == indices[inOrder[index]].level + 1) {
63                 nodes[levelOrder[i]]->right = nodes[inOrder[j]];
64                 break;
65             }
66         }
67     }
68     print(nodes[levelOrder[0]]);
69     cout << endl;
70     return 0;
71 }
时间: 2024-12-07 00:32:11

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 about this problems: 1. http://siddontang.gitbooks.io/leetcode-solution/content/tree/construct_binary_tree.html 2.http://blog.csdn.net/linhuanmars/artic

LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. Hide Tags Tree Array Depth-first Search SOLUTION 1: 使

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

44: Construct Binary Tree from Inorder and Postorder Traversal

/************************************************************************/            /*       44:  Construct Binary Tree from Inorder and Postorder Traversal                            */            /*************************************************

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 60461 Total Submissions: 203546 Difficulty: Medium Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not

LeetCode OJ - construct Binary Tree from Inorder and Postorder/Preorder Traversal

不断递归的实现!!!! 下面是AC代码: 1 /** 2 * Given inorder and postorder traversal of a tree, construct the binary tree. 3 * @param inorder 4 * @param postorder 5 * @return 6 */ 7 public TreeNode buildTree(int[] inorder,int[] postorder){ 8 if(inorder == null || po

Construct Binary Tree from Inorder and Postorder Traversal (算法课上的题)

Construct Binary Tree from Inorder and Postorder Traversal 这道题之前算法课上好像遇到过,思路也很简单的. 思路:后序序列的最后一个元素就是树根,然后在中序序列中找到这个元素(由于题目保证没有相同的元素,因此可以唯一找到),中序序列中这个元素的左边就是左子树的中序,右边就是右子树的中序,然后根据刚才中序序列中左右子树的元素个数可以在后序序列中找到左右子树的后序序列,然后递归的求解即可.(在去除了根节点之后,中序遍历和后序遍历的前N个树都是

Leetcode dfs Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 14363 Total Submissions: 54254My Submissions Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in t