由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教一个二叉树镜像问题
相关主题
Print a binary tree in level order but starting from leaf node up to root请教一道g算法题
一个GOOG的二叉树面试题一道面试题
判断(二叉)树是否镜像对称请教一道面试题
二叉树按层次打印有没有办法换行显示?刚刚电面bloomberg,被问到一个没看到过的问题
微软面试的一道题分享:non-recursive breadth first search and depth first search algorithm in C
问个二叉树删除结点的问题那个skiplist的题谁来给谢谢
求教一道面试题合并两个排序好的链表, 优解?
如何随机找二叉树中的任意节点?一道面试题:Flatten a multilevel linked list
相关话题的讨论汇总
话题: node话题: mirror话题: left话题: right话题: tree
进入JobHunting版参与讨论
1 (共1页)
t**n
发帖数: 272
1
Given a binary tree, how would you write program for getting mirror image of
tree in O(n) time? Is it possible ? Assume you have no constraints on space.
我只能想到递归:
Mirror(Node* p){
Mirror(p->left);
Mirror(p->right);
ExchangeLeftRight(p);
}
这个复杂度应该怎么算?
l*******t
发帖数: 642
2
Think how many times each node will be touched ...
b******v
发帖数: 1493
3
T(n)=2*T(n/2)+1
根据master theorem, 复杂度是O(n)

of
space.

【在 t**n 的大作中提到】
: Given a binary tree, how would you write program for getting mirror image of
: tree in O(n) time? Is it possible ? Assume you have no constraints on space.
: 我只能想到递归:
: Mirror(Node* p){
: Mirror(p->left);
: Mirror(p->right);
: ExchangeLeftRight(p);
: }
: 这个复杂度应该怎么算?

c******f
发帖数: 2144
4
mark
c******f
发帖数: 2144
5
这个应该等价二叉树的遍历
那么T(n)=2T(n/2)+O(1)
适用master theory的其中一种情况
当有ε > 0, 使得f(n)=O(n^(logb(a)-ε))则T(n)=Theta(n^logb(a))
这里a=2,b=2,logb(a)=1, f(n)=O(1), 所以存在epsilon=1
那么 T(n)=Theta(n)
z*******y
发帖数: 578
6
Check the code below:
void mirror(struct node* node) {
if (node==NULL) {
return;
}
else {
struct node* temp;
// do the subtrees
mirror(node->left);
mirror(node->right);
// swap the pointers in this node
temp = node->left;
node->left = node->right;
node->right = temp;
}
}
f**r
发帖数: 865
7
That's an O(N) algorithm (each node is only touched once). Be careful about
the boundary conditions though (i.e. check for NULL for p->left & p->right).

of
space.

【在 t**n 的大作中提到】
: Given a binary tree, how would you write program for getting mirror image of
: tree in O(n) time? Is it possible ? Assume you have no constraints on space.
: 我只能想到递归:
: Mirror(Node* p){
: Mirror(p->left);
: Mirror(p->right);
: ExchangeLeftRight(p);
: }
: 这个复杂度应该怎么算?

1 (共1页)
进入JobHunting版参与讨论
相关主题
一道面试题:Flatten a multilevel linked list微软面试的一道题
G家intern电面新鲜面经问个二叉树删除结点的问题
感觉今天结结实实被烙印阴了求教一道面试题
find treenode with two indegrees如何随机找二叉树中的任意节点?
Print a binary tree in level order but starting from leaf node up to root请教一道g算法题
一个GOOG的二叉树面试题一道面试题
判断(二叉)树是否镜像对称请教一道面试题
二叉树按层次打印有没有办法换行显示?刚刚电面bloomberg,被问到一个没看到过的问题
相关话题的讨论汇总
话题: node话题: mirror话题: left话题: right话题: tree