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

Google 店面面经,求米

全局:

2018(7-9月) 码农类General 硕士 全职@google - 内推 - 技术电面  | | Pass | 应届毕业生

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

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

x
9月份的店面面经
华人小哥,问的是给一个f
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
的森林和node,要求还原树

评分

参与人数 6大米 +14 收起 理由
salamanderrex1 + 1 很有用的信息!
bc2615 + 3 给你点个赞!
SimonLevy + 3 给你点个赞!
yvetteyt + 3 很有用的信息!
pandami + 1 赞一个

查看全部评分


上一篇:SDE new graduate UBER interview
下一篇:Google电面一面
推荐
 楼主| ZihaoZhai 2018-12-6 15:27:38 | 只看该作者
全局:
Reinn 发表于 2018-12-6 14:02
请问有如何还原bst的思路么?

当时时间不多了,想了个大概的思路,把每个点当成树,然后排个序,依照构建BST的操作来做

评分

参与人数 2大米 +6 收起 理由
Reinn + 3 很有用的面经思路
SimonLevy + 3 很有用的信息!

查看全部评分

回复

使用道具 举报

推荐
 楼主| ZihaoZhai 2018-12-6 13:01:23 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies

评分

参与人数 3大米 +9 收起 理由
yut210 + 3 很有用的信息!
SimonLevy + 3 给你点个赞!
youziwry + 3 很有用的信息!

查看全部评分

回复

使用道具 举报

全局:
  1. class TreeNode:
  2.     def __init__(self,val, left, right):
  3.         self.val = val
  4.         self.left = left
  5.         self.right = right


  6. badNodes = set()
  7. def isBadNode(node):
  8.     if node in badNodes:
  9.         return True
  10.     else:
  11.         return False

  12. def printTree(root):
  13.     def getHeight(root):
  14.         if root is None:
  15.             return 0
  16.         return 1 + max(map(getHeight, [root.left, root.right]))

  17.     def fill(root, i, l ,r):
  18.         if root is None:
  19.             return
  20.         res[i][(l+r)/2] = '' + str(root.val)
  21.         fill(root.left, i + 1, l, (l + r) / 2)
  22.         fill(root.right, i + 1, (l + r + 1) / 2, r)

  23.     height = getHeight(root)
  24.     res = [[''] * ((1 << height )-1) for _ in range(height)] # need to be (1<< hegith) -1 this () is needed

  25.     fill(root,0, 0, len(res[0]))
  26.     for x in res:
  27.         print x

  28.     return res

  29. def printForest(forest):
  30.     print 'print forest.......'
  31.     for tree in forest:
  32.         printTree(tree)
  33.         print '========'

  34. def trimTree(root):
  35.     forest = []
  36.     def helper(node, parent_added):
  37.         if node is None:
  38.             return None

  39.         if isBadNode(node):
  40.             helper(node.left, False)
  41.             helper(node.right, False)
  42.         else:
  43.             if not parent_added:
  44.                 forest.append(node)
  45.             helper(node.left, True)
  46.             helper(node.right, True)

  47.         # unlink
  48.         if isBadNode(node.left):
  49.             node.left = None
  50.         if isBadNode(node.right):
  51.             node.right = None

  52.     helper(root, False)
  53.     return forest




  54. node10 = TreeNode(10, None, None)
  55. node8  = TreeNode(8, None, None)
  56. node4 = TreeNode(4, None, None)
  57. node2 = TreeNode(2, None, None)
  58. node9 = TreeNode(9, None, None)
  59. node13 = TreeNode(13, None, None)
  60. node11 = TreeNode(11, None, None)
  61. node12 = TreeNode(12, None, None)

  62. node10.left = node8
  63. node8.right = node9
  64. node8.left = node4
  65. node4.left = node2

  66. node10.right = node13
  67. node13.left = node11

  68. node11.right = node12

  69. printTree(node10)


  70. #badNodes = set([node10, node8, node12])
  71. #badNodes = set([node10])
  72. badNodes = set([node8])

  73. forest = trimTree(node10)

  74. printForest(forest)



  75. def constructBST(badNodes, forest):
  76.     nodes = list(badNodes) + forest
  77.     nodes = sorted(nodes, key = lambda x: x.val)

  78.     print 'nodes in hand are', [n.val for n in nodes]

  79.     def dfs(nums, i, j):
  80.         if i > j:
  81.             return None
  82.         mid = (j - i) /2 + i
  83.         root = nums[mid]
  84.         left = dfs(nums, i, mid -1)
  85.         if left:
  86.             root.left = left
  87.         
  88.         right = dfs(nums, mid +1, j)
  89.         if right:
  90.             root.right = right
  91.         return root

  92.     root = dfs(nodes,0, len(nodes)-1)
  93.     printTree(root)
  94.     return root


  95. constructBST(badNodes, forest)
复制代码
回复

使用道具 举报

🔗
yvetteyt 2018-12-6 12:55:24 | 只看该作者
全局:
能稍微再详细点说下题目吗,谢谢楼主~
回复

使用道具 举报

🔗
yvetteyt 2018-12-6 13:13:21 | 只看该作者
全局:
ZihaoZhai 发表于 2018-12-6 00:01
就是给了一个function, input 一个node,output是T/F告诉树里面的这个node是不是应该被删除,删除之后子 ...

懂了,谢谢楼主~
回复

使用道具 举报

🔗
Reinn 2018-12-6 14:02:38 | 只看该作者
全局:
请问有如何还原bst的思路么?
回复

使用道具 举报

🔗
realsada2018 2018-12-6 15:36:53 | 只看该作者
全局:
两个题都有例子吗?
回复

使用道具 举报

🔗
 楼主| ZihaoZhai 2018-12-6 15:42:34 | 只看该作者
全局:
realsada2018 发表于 2018-12-6 15:36
两个题都有例子吗?

没有诶,当时口述的题,回答不易,求米哈
回复

使用道具 举报

🔗
Scala688 2018-12-9 12:02:48 | 只看该作者
全局:
        很有用的信息!
回复

使用道具 举报

🔗
littlegrasscao 2018-12-18 10:24:47 | 只看该作者
全局:
如果删除的node有左右子树 那删除后形成两个新的树?加上原来的一共三个树?
回复

使用道具 举报

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

本版积分规则

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