📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
楼主: gu0001hi
跳转到指定楼层
上一主题 下一主题
收起左侧

fb intern面经,面对奇葩问题膝盖碎了

🔗
Neal_kks 2014-11-25 14:50:14 | 只看该作者
全局:
这个题在做雅虎笔试的时候遇到过,我当时看成求层序遍历了,以为好简单。。用preorder递归应该是不行的,举个反例,假设第一个左孩子的右子树往右延伸了很长,那么在bfs他们都会在跟的右子树之前被加入到hash表,这是不对的。用层序遍历其实也不复杂,用个pair把他再水平方向的偏移量记录一下即可。贴一下我的实现,例子是楼主给出的那个。大家看看有没有bug。
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <queue>
  5. #include <vector>
  6. using namespace std;

  7. struct tree{
  8.     string key;
  9.     tree *left, *right;
  10.     tree(string mkey):key(mkey), left(NULL), right(NULL){}
  11. };

  12. typedef pair<tree*, int> pti;

  13. map<int, vector<tree*> > dict;

  14. void build(tree *root){
  15.     if(root == NULL){
  16.         return;
  17.     }
  18.     queue<pti> que;
  19.     que.push(pti(root, 0));
  20.     while(!que.empty()){
  21.         pti cur = que.front();
  22.         que.pop();
  23.         dict[cur.second].push_back(cur.first);
  24.         if(cur.first->left){
  25.             que.push(pti(cur.first->left, cur.second-1));
  26.         }
  27.         if(cur.first->right){
  28.             que.push(pti(cur.first->right, cur.second+1));
  29.         }
  30.     }
  31. }

  32. void solve(tree *root){
  33.     if(root == NULL){
  34.         return;
  35.     }
  36.     build(root);
  37.     map<int, vector<tree*> >::const_iterator it = dict.begin();
  38.     while(it != dict.end()){
  39.         vector<tree*>::const_iterator itt = it->second.begin();
  40.         while(itt != it->second.end()){
  41.             cout<<(*itt)->key<<"\t";
  42.             ++itt;
  43.         }
  44.         cout<<endl;
  45.         ++it;
  46.     }
  47. }

  48. int main(){
  49.     tree *t1 = new tree("a");
  50.     tree *t2 = new tree("b");
  51.     tree *t3 = new tree("c");
  52.     tree *t4 = new tree("d");
  53.     tree *t5 = new tree("e");
  54.     tree *t6 = new tree("g");
  55.     tree *t7 = new tree("z");
  56.     tree *t8 = new tree("i");
  57.     tree *t9 = new tree("q");
  58.     tree *t10 = new tree("x");
  59.     tree *t11 = new tree("x1");
  60.     tree *t12 = new tree("x2");
  61.     t1->left = t2;
  62.     t1->right = t3;
  63.     t2->left = t4;
  64.     t2->right = t6;
  65.     t4->right = t5;
  66.     t3->right = t7;
  67.     t7->left = t8;
  68.     t8->left = t9;
  69.     t9->left = t10;
  70.     t10->left = t11;
  71.     t11->left = t12;
  72.     solve(t1);
  73. }

  74. /*
  75.       a
  76.      / \
  77.     b   c
  78.    / \   \
  79.   d   g   z
  80.    \     /
  81.     e   i
  82.        /
  83.       q
  84.      /
  85.     x
  86.    /
  87.   x1
  88. /
  89. x2
  90. */

复制代码
回复

使用道具 举报

🔗
尚佳蕾 2015-2-5 21:42:56 | 只看该作者
全局:
your output is like this omg!

x2       
d        x1       
b        e        x       
a        g        q       
c        i       
z       
回复

使用道具 举报

🔗
Larrylianj 2015-2-9 14:18:18 | 只看该作者
全局:
当年也正派 发表于 2014-3-4 09:45
我的想法是用hashmap, key是列的偏移,设定root偏移为0,然后向左走偏移-1,向右偏移+1,value是一个Strin ...

preorder是不行,会打乱顺序,必须得BFS
回复

使用道具 举报

🔗
Larrylianj 2015-2-9 14:20:18 | 只看该作者
全局:
kongweihan 发表于 2014-11-24 08:41
请问BFS如何知道当前节点的vertical_index呢?

用一个map存偏移量,Map<TreeNode, Integer>
回复

使用道具 举报

🔗
CrossTheWall 2015-2-9 20:54:22 | 只看该作者
全局:
这题虽然略蛋疼但还比较容易,preorder traversal tree,用unordered_map<int,  vector<TreeNode*> >来保存每个key相同的结点集合,根的key是0,左树加1, 右树减1。 key是正是负都无所谓
回复

使用道具 举报

全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

🔗
ex172000 2015-9-23 03:29:07 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

🔗
calvinhmw 2015-9-23 12:52:39 | 只看该作者
全局:
Neal_kks 发表于 2014-11-25 14:50
这个题在做雅虎笔试的时候遇到过,我当时看成求层序遍历了,以为好简单。。用preorder递归应该是不行的,举 ...

dict 里的 vector<Tree*> 应该初始化一下吧?
回复

使用道具 举报

🔗
mmliu 2015-9-24 17:24:43 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

🔗
szm9119 2015-9-28 03:56:41 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

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

本版积分规则

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