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

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

全局:

2014(1-3月) 码农类General 硕士 实习@meta - 校园招聘会 - 技术电面  | | Fail |

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

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

x
已跪,45分钟面了道很奇葩的问题,面试是个三哥,有气无力地听着,最后也装模作样回答一些问题.. 跪了
问题:
Print arbitrary binary tree by vertical levels / columns from left to right


example tree

      a
     / \
    b   c
   / \   \
  d   g   z
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
不是可以把<key, string>这样存

补充内容 (2014-3-4 06:02):
因为他要求的只是打印出来,不需要有data structure可以存所有的nodes.. 这样可以达到题目要求,可是总觉得不是很好,有点投机取巧,求大大们解释

评分

参与人数 4大米 +69 收起 理由
chemsslu + 1 感谢分享!
mmliu + 3 感谢分享!
wy193777 + 5 感谢分享!
豌豆开心果 + 60

查看全部评分


上一篇:Teradata and Amazon 面试总结,积攒人品
下一篇:Medallia面经

本帖被以下淘专辑推荐:

  • · fb|主题: 33, 订阅: 14
推荐
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. */

复制代码
回复

使用道具 举报

全局:
我的想法是用hashmap, key是列的偏移,设定root偏移为0,然后向左走偏移-1,向右偏移+1,value是一个String的arraylist。然后应该和lz想法就差不多了,preorder遍历,每个node的值append到对应的偏移的arraylist后面
回复

使用道具 举报

全局:
这题虽然略蛋疼但还比较容易,preorder traversal tree,用unordered_map<int,  vector<TreeNode*> >来保存每个key相同的结点集合,根的key是0,左树加1, 右树减1。 key是正是负都无所谓
回复

使用道具 举报

🔗
austurela 2014-3-4 07:07:44 | 只看该作者
全局:
How is this?
  1. import java.util.ArrayList;

  2. public class PrintByColumn<T> {
  3.        
  4.         public void Print(TreeNode<T> root){
  5.                 ArrayList<ArrayList<T>> right = new ArrayList<ArrayList<T>>();
  6.                 ArrayList<ArrayList<T>> left = new ArrayList<ArrayList<T>>();
  7.                 printHelper(0, root, left, right);
  8.                 // Print result
  9.                 for(int i = left.size() - 1; i > 0; i--){
  10.                         System.out.println(left.get(i));
  11.                 }
  12.                 for(int i = 0; i < right.size(); i++){
  13.                         System.out.println(right.get(i));
  14.                 }
  15.         }
  16.        
  17.         private void printHelper(int index, TreeNode<T> root, ArrayList<ArrayList<T>> left, ArrayList<ArrayList<T>> right){
  18.                 // Base case
  19.                 if(root == null)        return;
  20.                 // Normal case
  21.                 if(index >= 0){
  22.                         while(right.size() <= index){
  23.                                 right.add(new ArrayList<T>());
  24.                         }
  25.                         right.get(index).add(root.data);
  26.                 }
  27.                 else{
  28.                         while(left.size() <= -index){
  29.                                 left.add(new ArrayList<T>());
  30.                         }
  31.                         left.get(-index).add(root.data);
  32.                 }
  33.                 // Recurse
  34.                 printHelper(index - 1, root.left, left, right);
  35.                 printHelper(index + 1, root.right, left, right);
  36.         }
  37. }
复制代码
Testing:
  1. import java.util.*;

  2. class TreeNode<T>{
  3.         TreeNode<T> left;
  4.         TreeNode<T> right;
  5.         T data;
  6.        
  7.         TreeNode(T data){
  8.                 this.data = data;
  9.         }
  10. }

  11. public class Test {
  12.         public static void main(String[] args){
  13.                 TreeNode<String> root = new TreeNode<String>("a");
  14.                 root.left = new TreeNode<String>("b");
  15.                 root.right = new TreeNode<String>("c");
  16.                
  17.                 root.left.left = new TreeNode<String>("d");
  18.                 root.left.right = new TreeNode<String>("g");
  19.                
  20.                 root.right.right = new TreeNode<String>("z");
  21.                 root.right.right.left = new TreeNode<String>("i");
  22.                 root.right.right.left.left = new TreeNode<String>("q");
  23.                 root.right.right.left.left.left = new TreeNode<String>("x");
  24.                 root.right.right.left.left.left.left = new TreeNode<String>("x1");
  25.                 root.right.right.left.left.left.left.left = new TreeNode<String>("x2");
  26.                
  27.                 root.left.left.right = new TreeNode<String>("e");
  28.                
  29.                 PrintByColumn<String> test = new PrintByColumn<String>();
  30.                 test.Print(root);
  31.         }
  32. }
复制代码




补充内容 (2014-3-4 07:08):
Output:
[x2]
[d, x1]
[b, e, x]
[a, g, q]
[c, i]
[z]

评分

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

查看全部评分

回复

使用道具 举报

🔗
nealaws 2014-3-4 09:25:32 | 只看该作者
全局:
跟求bs的最大深度一样 用recursion求完深度以后加到深度的arraylist即可

补充内容 (2014-3-4 09:32):
貌似不对 还得想想

补充内容 (2014-3-4 09:45):
我的做法跟lz 一样 左面key减1右面key加1 然后用hashmap 保存 key linkedlist 和最大最小值 然后iterate
回复

使用道具 举报

🔗
lixiang.xjtu 2014-3-4 11:08:49 | 只看该作者
全局:
弄个链表不行吗。正的往右,负的往左。
或者弄俩数组,一个存正的,一个存负的。
回复

使用道具 举报

全局:
当年也正派 发表于 2014-3-4 09:45
我的想法是用hashmap, key是列的偏移,设定root偏移为0,然后向左走偏移-1,向右偏移+1,value是一个Strin ...

我觉得也是,HashMap<Integer,ArrayList<TreeNode>>用来保存对应的key对应的节点,更新最小key value就行了。然后iterate hashmap,比如key的最小是-5,那么说明root左边有5层,那么key=-5 to key not in HashMap,依次print HashMap中的ArrayList
回复

使用道具 举报

🔗
WinterNight 2014-3-5 08:41:22 | 只看该作者
全局:
想法差不多,BFS。 HashMap<Integer, ArrayList<Integer>> , 存一下最大最小列数
=========
key   value
0      a -> g -> q
-1      b -> e -> x
1      c -> i
-2      d -> x1
2      z
-3      x2
回复

使用道具 举报

无效楼层,该帖已经被删除
🔗
lingeast 2014-3-5 11:50:25 | 只看该作者
全局:
用vector<vector<Node*>> tree_list 结构是可以存的。做一个pre-order traverse,把node放到相应的level中,因为是pre-order,所以tree_list.size()始终等于 depth 或 depth + 1。
回复

使用道具 举报

🔗
tyr034 2014-10-29 00:43:51 | 只看该作者
全局:
lingeast 发表于 2014-3-5 11:50
用vector tree_list 结构是可以存的。做一个pre-order traverse,把node放到相应的level中,因为是pre-orde ...

pre-ordery 应该不行把,因为会把level的顺序弄乱。
虽然题主给的case这题可以用Pre-order,但我觉得是特例。
得 Breadth first search。
回复

使用道具 举报

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

本版积分规则

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