查看: 2389| 回复: 12
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第三轮】7.14-7.20 CareerCup 4.8

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
4.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 Tl.
A tree T2 is a subtree of Tl if there exists a node n in Tl such that the subtree of n is identical to T2. That is, if you cut off the tree at node n, the two trees would be identical.

回复解法可以按照以下格式来
【解题思路】
【时间复杂度】
【空间复杂度】
【gist link】
---------------Optional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】


Notice:
1、记得在程序注释中表明自己算法的时间、空间复杂度
2、代码难懂之处加注释
3、每道题目有对应的帖子,除了贴解法,欢迎讨论,集思广益
4、任何未尽之处,欢迎回报名帖提问,我会进一步作出修改。


上一篇:【第三轮】7.14-7.20 CareerCup 4.7
下一篇:【第三轮】7.14-7.20 CareerCup 4.9
推荐
donnice 2014-7-18 04:59:52 | 只看该作者
全局:
【解题思路】
check if b1 has the root of b2, if not, return false; if true, check the value of nodes one by one(here use an arraylist)
【时间复杂度】
O(mn)
【空间复杂度】
O(n)
【gist】
https://github.com/donnice/donnice/blob/master/Q4_8
【Test】
                     5                                                               
                2                  8                              
          1          4       7                                          
                3         6   
回复

使用道具 举报

全局:
好久不来,上次说暂时休息后就一下子拉下来了 >.< 继续
【解题思路】
for all nodes in T1 that the same as the root of T2, check if this is the subtree of T1 that the same as T2
【时间复杂度】
O(n+km)
worst case : O(m*n)
ps: n--Node# in t1
     m--Node# in t2
      k--occurence of t2 root in t1
【空间复杂度】
O(log m + log n)  for recursion calls
【gist link】 https://gist.github.com/JoyceeLee/690e6bba1b4631aa9fff
回复

使用道具 举报

推荐
jyh橘子 2014-7-18 05:15:30 | 只看该作者
全局:
【解题思路】
Firstly, do pre-order traversal for T1 to find a node equals to the root of T2;
Then check if this subtree of T1 is the same with tree T2;
【时间复杂度】
O(n+km)
where n is the number of nodes in T1, m is the number of nodes in T2, k is the number of nodes in T1 that are equal to the root of T2;
The worst case is O(m*n)
【空间复杂度】
O(log m + log n)  for recursion calls
【gist link】 https://gist.github.com/jyhjuzi/2c5192865cd2fefdaaf6

回复

使用道具 举报

🔗
grassgigi 2014-7-15 09:35:18 | 只看该作者
全局:
【解题思路】
Recursion:
Check is subtree from root node.
If not recursively call function on both left subtree and right subtree.

【时间复杂度】
O(N*M) -  N is large tree, M is the one with small amount of nodes

【空间复杂度】
O(Height of N + Height of M)

【gist link】
https://gist.github.com/chrislukkk/905c242ada37ac3caf95
Helper class: https://gist.github.com/chrislukkk/2a1f825b8e924ea773e8
回复

使用道具 举报

全局:
【解题思路】
traverse T1 by level order
check whether the value of nodes in T1 and T2 equal each other.

【时间复杂度】
O(mn) worst case


【空间复杂度】
O(logm + logn)



【gist link】
https://gist.github.com/happyWinner/00db927f38cd2955e3cf

评分

参与人数 1大米 +3 收起 理由
kimiflasky + 3 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
renli3000 2014-7-18 09:12:52 | 只看该作者
全局:
【解题思路】
一开始被百万的数据量给搞糊涂了,以为一棵树装不到内存里必须要拆分啊有木有
后来无耻的翻了下书,发现其实思路和自己开始想的是一样的,都是先比较root,然后再比较subtree,然后就是写2个简单的第归
另外书上第一种解法好奇葩有木有。。。
【时间复杂度】
O(mn)
【空间复杂度】
O(n)
【gist】
https://gist.github.com/Noahsark/2b021566d3862b4bb20c
回复

使用道具 举报

🔗
bitcpf 2014-7-18 23:33:03 | 只看该作者
全局:
【解题思路】Recursion: if the subtree node is in the tree, when find the node then check if it is the subtree; else check the left and right side of the tree.

【时间复杂度】
O(N*M) -  N is large tree, M is the one with small amount of nodes

【空间复杂度】
O(N)

【gist link】https://gist.github.com/bitcpf/b85b3ee344637593a88d
回复

使用道具 举报

🔗
ivycheung1208 2014-7-19 02:16:24 | 只看该作者
全局:
【解题思路】
search for T2's root in T1, call the helper function to determine whether it is a subtree.
【时间复杂度】
O(N+kM)
worst case: O(MN)
【空间复杂度】
O(logM+logN)
【gist link】
https://gist.github.com/1575900cb5e3e74211bc
【test case】
pay attention to empty tree cases
回复

使用道具 举报

🔗
林微熙 2014-7-19 08:17:12 | 只看该作者
全局:
【解题思路】Compare root compare subtree then recursion
【时间复杂度】o(mn)
【空间复杂度】o(n)
【gist link】https://gist.github.com/hilda8519/794e24eb177b5523698e
回复

使用道具 举报

🔗
tonygxxx1212 2014-7-19 23:22:08 | 只看该作者
全局:

【解题思路】Compare root compare subtree then recursion
【时间复杂度】o(n+km) k is how many times we check subtree  , n for T2, m for T1
【空间复杂度】o(n)
【gist link】https://gist.github.com/xun-gong/178f0cdc80688dd09b84
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表