楼主: 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是正是负都无所谓
回复

使用道具 举报

全局:
A solution:  find the leftmost and rightmost level first. Then print each level using recursion.

        public static void main(String[] args) {
                int[] min = new int[1];
                getLeftmostIndex(a, 0, min);
               
                int[] max = new int[1];
                getRightmostIndex(a, 0, max);
               
                for (int level = min[0]; level <= max[0]; ++level) {
                        List<Integer> list = new ArrayList<Integer>();
                        getOneLevel(a, 0, level, list);
                        System.out.println(list);
                }
        }
       
        public static void getLeftmostIndex(TreeNode root, int curIndex, int[] min) {
                if (root == null) return;
                min[0] = Math.min(min[0], curIndex);
                getLeftmostIndex(root.left, curIndex - 1, min);
                getLeftmostIndex(root.right, curIndex + 1, min);
        }
       
        public static void getRightmostIndex(TreeNode root, int curIndex, int[] max) {
                if (root == null) return;
                max[0] = Math.max(max[0], curIndex);
                getRightmostIndex(root.left, curIndex - 1, max);
                getRightmostIndex(root.right, curIndex + 1, max);               
        }
       
        public static void getOneLevel(TreeNode root, int cur,  int level, List<Integer> list) {
                if (root == null) return;
                if (cur == level) list.add(root.val);
                getOneLevel(root.left, cur - 1, level, list);
                getOneLevel(root.right, cur + 1, level, list);       
        }
回复

使用道具 举报

🔗
ex172000 2015-9-23 03:29:07 | 只看该作者
全局:
一亩三分地严打"顶""好贴""收藏了"之类的垃圾回复帖!被警告三次,系统会自动封杀ID!

想支持楼主,请点击帖子下方的"好苗""分享""收藏"键,酌情给楼主加大米(系统不扣你自己的分)。
积分不够看不了帖子,请参考论坛导航里的"帮助","新手提纲"里有攒积分指南
  1. import java.util.*;
  2. public class Solution {
  3.     Map<Integer, List<String>> map = new TreeMap<Integer, List<String>>();
  4.     public List<List<String>> verticalPrint(Tree root) {
  5.         List<List<String>> result = new ArrayList<List<String>>();
  6.         if (root != null) radd(root, 0);
  7.         for (int i : map.keySet()) result.add(map.get(i));
  8.         return result;
  9.     }

  10.     public void radd(Tree root, int x) {
  11.         if (map.containsKey(x)) map.get(x).add(root.key);
  12.         else {
  13.             List<String> tmp = new ArrayList<>();
  14.             tmp.add(root.key);
  15.             map.put(x, tmp);
  16.         }
  17.         if (root.left  != null) radd(root.left, x-1);
  18.         if (root.right != null) radd(root.right, x+1);
  19.     }
  20. }
复制代码
回复

使用道具 举报

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

赞,看起来挺奇葩的题,想通了也挺常规,关键是不要怕
回复

使用道具 举报

🔗
szm9119 2015-9-28 03:56:41 | 只看该作者
全局:
这题最naive的方法应该是traverse, 给每个node有设置对应的vertical level, E.g. root = 0, root.left = -1. root.right = 1.

补充内容 (2015-9-28 03:59):
然后另外维护一个treemap<int, node>, 从vertical level 映射到node。最后讲treemap按vertical level从小到大,按顺序输出

补充内容 (2015-9-28 04:03):
其实也不需要treemap, 直接用hashmapc存即可,只是需要记录vertical index的范围
回复

使用道具 举报

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

本版积分规则

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