由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 两个二叉树,找出最大的相同子树
相关主题
serialize tree可否用in order或者post order问道题
non recursive binary tree traversal in O(n) time and O(1) spaceB家面筋
convert bst to doubly linked list 求个干净容易理解的答案求教一个combination的问题,求好方法
MS面试题判断一个linked list是不是palindrome
微软面试的一道题[面试题] 如何打印一个二叉树level by level?
判断(二叉)树是否镜像对称二叉树按层次打印有没有办法换行显示?
问一道二叉树serialize的问题facebook三轮technical phone interview,要崩溃了
找二叉树 两个最大的相同子树merge two binary search tree
相关话题的讨论汇总
话题: t2话题: match话题: tree话题: t1话题: return
进入JobHunting版参与讨论
1 (共1页)
b******g
发帖数: 1721
1
我现在想到的就是
1. 先遍历t1 and t2, 找到他们的size,
2. 假设t2比较小,然后就是看t1有无t2
3. 那么我的算法就是
match(Tree t1, Tree t2){
if(t1.value=t2.value)
return match(t1.left,t2.left) && match(t1.right,t2.right);
else
return match(t1.left,t2) || match(t1.right,t2);
}
complexity |t2|*|t1|
有无更好的算法?
p*****2
发帖数: 21240
2

code感觉不是很对呀。不过复杂度也应该就是这个了。没什么好办法。不过这个复杂度
是worst case。

【在 b******g 的大作中提到】
: 我现在想到的就是
: 1. 先遍历t1 and t2, 找到他们的size,
: 2. 假设t2比较小,然后就是看t1有无t2
: 3. 那么我的算法就是
: match(Tree t1, Tree t2){
: if(t1.value=t2.value)
: return match(t1.left,t2.left) && match(t1.right,t2.right);
: else
: return match(t1.left,t2) || match(t1.right,t2);
: }

r*******n
发帖数: 3020
3
相同是指子树的结点个数相同?

【在 b******g 的大作中提到】
: 我现在想到的就是
: 1. 先遍历t1 and t2, 找到他们的size,
: 2. 假设t2比较小,然后就是看t1有无t2
: 3. 那么我的算法就是
: match(Tree t1, Tree t2){
: if(t1.value=t2.value)
: return match(t1.left,t2.left) && match(t1.right,t2.right);
: else
: return match(t1.left,t2) || match(t1.right,t2);
: }

w**z
发帖数: 8232
4
What is your base step?
You need to remember the match t1 node (Let's mark it MatchRoot) the as t2'
root.
If you find the unmatch, you need to back from the previous matching root (
MatchRoot) and going down again from its children.
b******g
发帖数: 1721
5
相同是说不光结构相同,而且每个对应的value也相同。根据peking2 和 wwzz建议,我
给个全的,也是个大概,可能边角没有考虑。
int matchRoot(Tree t1, Tree t2)
{
Queue q1=new Queue();
q1.enqueue(t2);
int size=0;
while(q1.size()){
Tree tmp=q1.dequeue();
if(!match(t1,tmpTree)){
if(!tmp.left)
q1.enqueue(tmp.left);
if(!tmp.right)
q1.enqueue(tmp.right);
}else{
size=tmp.size();
}

}
return size;
}
match(Tree t1, Tree t2){
if(t1.value=t2.value)
return match(t1.left,t2.left) && match(t1.right,t2.right);
else
return match(t1.left,t2) || match(t1.right,t2);
}
w**z
发帖数: 8232
6
You don't have your base step for your recursive match method. Your method
runs in to NPE eventually
Need to check t1.left/right, t2.left/righ == null. All of null, return true.
If children structure mismatch, (t1 has left, t2 doesn't, etc) return false.
If children structure match, recursively calling match with non-null
children
1 (共1页)
进入JobHunting版参与讨论
相关主题
merge two binary search tree微软面试的一道题
重建二叉树 from inorder and level order判断(二叉)树是否镜像对称
求问把二叉树的recursive遍历改为stack实现的思路问一道二叉树serialize的问题
请问BST里怎么处理“等于”的情况?找二叉树 两个最大的相同子树
serialize tree可否用in order或者post order问道题
non recursive binary tree traversal in O(n) time and O(1) spaceB家面筋
convert bst to doubly linked list 求个干净容易理解的答案求教一个combination的问题,求好方法
MS面试题判断一个linked list是不是palindrome
相关话题的讨论汇总
话题: t2话题: match话题: tree话题: t1话题: return