12
返回列表 发新帖
楼主: wrj5518
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第三轮】7.7-7.13 CareerCup 4.6

🔗
林微熙 2014-7-16 10:57:36 | 只看该作者
全局:
【解题思路】1. if the node has a right child, return the leftmost node of its right subtree
2. else if the node is the left child of its parent, return its parent
3. else (the node is the right child of its parent) then keep searching upwards the tree until the situation of (2) is found
【时间复杂度】o(n)
【空间复杂度】o(n)
【gist link】https://gist.github.com/hilda8519/b5f7b62a0ae43c9a98f1
回复

使用道具 举报

🔗
jyh橘子 2014-7-17 06:30:04 | 只看该作者
全局:
【解题思路】
two cases:
1.  node has a right child.  Then the next node is the most left node of its right subtree;
2.  node has no right child.  Then we need to find the first ancestor that is the left child of its own parent.  then the parent is exactly the node we need.  If we reach root before find the needed node, we should return null which means there is no next node.
【时间复杂度】 O(H)
【空间复杂度】 O(1)
【gist link】 https://gist.github.com/jyhjuzi/5d88cc27d9b91c763dcb
回复

使用道具 举报

🔗
renli3000 2014-7-18 06:50:58 | 只看该作者
全局:
本帖最后由 renli3000 于 2014-7-18 07:14 编辑

【解题思路】
  case1: nood.right == null : traverse to father untile node != node.father.right
  case2: nood.right != null: traverse to right, and find the minimum
其实不用father指针更好做,从root往下找,key小了就记下指针往左,key大了就往右走

【时间复杂度】 O(H)
【空间复杂度】 O(1)
【gist link】
https://gist.github.com/Noahsark/7124940b34eccc34a31a

回复

使用道具 举报

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

【解题思路】
1.  node has a right child.  Then the next node is the most left node of its right subtree;
2.  node has no right child && node <= its parent, next node = parent
2.  node has no right child && node > its parent, keep search for parent of parent until find, else return NULL
【时间复杂度】 O(logn)
【空间复杂度】 O(1)
【gist link】https://gist.github.com/xun-gong/f4497902c29bc0c1cdf5
回复

使用道具 举报

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

本版积分规则

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