由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Cracking coding interview里的一道例题
相关主题
这个Binary Tree的题来看看Interview question for Quant to share-3, please discuss and help each other
linked list vs Binary tree[合集] 请问binary searth tree的遍历问题。
BST查找next lowest 可以达到 O(lg N)? (转载)请教算法题
求教:取串中的子串好方法react js这个有必要学习么?是代替什么的?干angular js用的?
BFS traversal starting from leaf level ???教授的问题(C language)
两个关于matrix的问题请教大虾别骂我,请推荐c++书
请教一道新奇的面试题静观java,scala,c#,js牛人抢答
一个算法问题Android论坛框架
相关话题的讨论汇总
话题: t2话题: t1话题: s2话题: s1话题: inorder
进入Programming版参与讨论
1 (共1页)
y****n
发帖数: 15
1
题目描述 Q4.8:
You have two very large binary trees: T1, with millions of nodes, and T2,
with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of
T1.
书中的解法一:
If T2’s preorder traversal is a substring of T1’s preorder traversal, and
T2’s inorder traversal is a substring of T1’s inorder traversal, then T2
is a subtree of T1.
我感觉这个方法可能有问题。虽然通过preorder和inorder遍历,可以唯一的定义一棵
树。但是如果T2是T1的子树,遍历字符串S2是S1的子串,那么S2可能对应到S1的不同位
置。举例来说,下面两棵树就满足S2是S1的子串,但T2不是T1的子树。
请众位高手帮忙检查一下,是我的结果有问题,还是书中有错误,多谢!
2 2
/ \ / \
1 2 1 3
/ / \
3 4 3
/ \
5 1
Preorder: S1=21324513 S2=213
Inorder: S1=31254123 S2=123
m*******l
发帖数: 12782
2
书错了

of
and

【在 y****n 的大作中提到】
: 题目描述 Q4.8:
: You have two very large binary trees: T1, with millions of nodes, and T2,
: with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of
: T1.
: 书中的解法一:
: If T2’s preorder traversal is a substring of T1’s preorder traversal, and
: T2’s inorder traversal is a substring of T1’s inorder traversal, then T2
: is a subtree of T1.
: 我感觉这个方法可能有问题。虽然通过preorder和inorder遍历,可以唯一的定义一棵
: 树。但是如果T2是T1的子树,遍历字符串S2是S1的子串,那么S2可能对应到S1的不同位

1 (共1页)
进入Programming版参与讨论
相关主题
Android论坛框架BFS traversal starting from leaf level ???
也问个regular expression的问题两个关于matrix的问题请教
About Longest repeated substring请教一道新奇的面试题
one question about algorithm一个算法问题
这个Binary Tree的题来看看Interview question for Quant to share-3, please discuss and help each other
linked list vs Binary tree[合集] 请问binary searth tree的遍历问题。
BST查找next lowest 可以达到 O(lg N)? (转载)请教算法题
求教:取串中的子串好方法react js这个有必要学习么?是代替什么的?干angular js用的?
相关话题的讨论汇总
话题: t2话题: t1话题: s2话题: s1话题: inorder