由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问个1337网页上面的经典题
相关主题
Construct Binary Tree from Inorder and Postorder TraversalG家新鲜面经
问几个有关Binary tree的题F家phone interview的一道题
问一道算法题Tree的traversal也分BFS和DFS?
攒人品,amazon一面经历一个电面疑问
问一道二叉树遍历的问题? 谢谢!全部答出来了,还是被amazon onsite据了
[leetcode] Binary Tree from Inorder & Postorder Traversalreconstruct the tree from inorder and postorder lists
LeetCode题Binary Tree Inorder Traversalpreorder/postorder和inorder重建树有非递归的方法吗?
讨论一道leetcode上面的题考大家个新题 visitor reconstruct generic tree
相关话题的讨论汇总
话题: int话题: mapindex话题: inorder话题: max
进入JobHunting版参与讨论
1 (共1页)
s*****y
发帖数: 897
1
Construct Binary Tree From Inorder and Preorder/Postorder Traversal
const int MAX = 256;
// a fast lookup for inorder's element -> index
// binary tree's element must be in the range of [0, MAX-1]
int mapIndex[MAX];
void mapToIndices(int inorder[], int n) {
for (int i = 0; i < n; i++) {
assert(0 <= inorder[i] && inorder[i] <= MAX-1);
mapIndex[inorder[i]] = i;
}
}
// precondition: mapToIndices must be called before entry
Node *buildInorderPreorder(int in[], int pre[], int n, int offset) {
assert(n >= 0);
if (n == 0) return NULL;
int rootVal = pre[0];
int i = mapIndex[rootVal]-offset; // the divider's index
Node *root = new Node(rootVal);
root->left = buildInorderPreorder(in, pre+1, i, offset);
root->right = buildInorderPreorder(in+i+1, pre+i+1, n-i-1, offset+i+1);
return root;
}
如果有2个node的元素值一样,这个用hash保存mapIndex是不是就不行了,一定要每次
慢慢找,或者用chaining建立hash?
还有buildInorderPreorder 这个函数传进去的参数int in[]似乎在建立了mapIndex就
没有用了,根本不需要传这个进去。
1 (共1页)
进入JobHunting版参与讨论
相关主题
考大家个新题 visitor reconstruct generic tree问一道二叉树遍历的问题? 谢谢!
现在怎么都是讨论offer的,没有做题的了?[leetcode] Binary Tree from Inorder & Postorder Traversal
LeetCode construct Binary TreeLeetCode题Binary Tree Inorder Traversal
Amazon 电面讨论一道leetcode上面的题
Construct Binary Tree from Inorder and Postorder TraversalG家新鲜面经
问几个有关Binary tree的题F家phone interview的一道题
问一道算法题Tree的traversal也分BFS和DFS?
攒人品,amazon一面经历一个电面疑问
相关话题的讨论汇总
话题: int话题: mapindex话题: inorder话题: max