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

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

全局:

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

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

x
4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree.

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


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



上一篇:北京大学暑期课:ACM/ICPC竞赛训练课件
下一篇:【第三轮】7.14-7.20 CareerCup 4.8
全局:
【解题思路】Assumption: when p is one ancestor of q's, we say p is the first common ancestor of p, q
                  Recursion to solve this problem
                      Case 1: p, q locate in different side of root, first common ancestor must be root
                      Case 2: p, q locates in same side of root
                                 sub-case-1: p,q both in left of root , focus on left subtree and recurse on it
                                 sub-case-2: p,q both in right of root, focus on right subtree and recurse on it
                      判断p, q 在不在某subtree也用到了recursion,做的时候没想到,看了答案才知道的
                      整道题就是逻辑+递归,代码量很少,想着费脑
【时间复杂度】O(n)
【空间复杂度】O(h)
【gist link】https://gist.github.com/xun-gong/05fcd99c19eb6b6b1e85
回复

使用道具 举报

推荐
donnice 2014-7-18 02:52:44 | 只看该作者
全局:
【解题思路】
recursion

【时间复杂度】
O(n)
【空间复杂度】
O(h)
【gist link】
https://github.com/donnice/donni ... f40876c5374f77c35dc
【Test】
                      5                                                               
               2                  8                              
         1          4       7                                          
               3         6   
回复

使用道具 举报

🔗
grassgigi 2014-7-15 09:29:42 | 只看该作者
全局:
本帖最后由 grassgigi 于 2014-7-15 09:35 编辑

【解题思路】
Recursion

【时间复杂度】
O(N^2) in worst case

【空间复杂度】
O(Height of N) for call stack

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

使用道具 举报

全局:
【解题思路】
recursion


【时间复杂度】
O(n)

【空间复杂度】
O(h)

【gist link】
https://gist.github.com/happyWinner/58853a40c12679d44fb2




回复

使用道具 举报

🔗
兰橘清檬 2014-7-16 02:34:51 | 只看该作者
全局:
【解题思路】
recursion 测试是否包含两个节点,然后再确定是 root 还是 其子结点
【时间复杂度】
O(n)
【空间复杂度】
O(?)
【gist link】
https://gist.github.com/JoyceeLee/765c93b3f1c130fc5501
回复

使用道具 举报

🔗
林微熙 2014-7-16 13:12:16 | 只看该作者
全局:
【解题思路】find a chain p,q on same side. 书里的
(1)p,q are on left side, branch left to look for common ancestor
(2)p,q are on right side, branch right to look for common ancestor
(3)p,q are no longer on same side, find first common ancestor
【时间复杂度】o(n)
【空间复杂度】o(n)
【gist link】https://gist.github.com/hilda8519/fe8866902dc1705e7899
回复

使用道具 举报

🔗
bitcpf 2014-7-17 00:36:01 | 只看该作者
全局:
【解题思路】recursion
if root is null, return null
if root is q/p, return root
if, p,q in different sides, return root
if p,q in the same side, check that side to see if p,q on different sides
【时间复杂度】o(n)
【空间复杂度】o(n)
【gist link】https://gist.github.com/happyWinner/58853a40c12679d44fb2
回复

使用道具 举报

🔗
jyh橘子 2014-7-17 08:13:59 | 只看该作者
全局:
【解题思路】recursion
first check if the tree covers both the two nodes, if not, return false
recursive function:
1. if contains node1 only, return node1
2. if contains node2 only, return node2
3. if contains both, return the first ancestor
【时间复杂度】o(n)
【空间复杂度】o(H) for recursion calls
【gist link】https://gist.github.com/jyhjuzi/f3f546249eb93d3adb7b
回复

使用道具 举报

🔗
jyh橘子 2014-7-17 08:14:06 | 只看该作者
全局:
本帖最后由 jyh橘子 于 2014-7-17 08:16 编辑

【解题思路】recursion
first check if the tree covers both the two nodes, if not, return false
recursive function:
1. if contains node1 only, return node1
2. if contains node2 only, return node2
3. if contains both, return the first ancestor
【时间复杂度】o(n)
【空间复杂度】o(H) for recursion calls
【gist link】https://gist.github.com/jyhjuzi/f3f546249eb93d3adb7b
回复

使用道具 举报

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

本版积分规则

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