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

Facebook Phone Interview

🔗
stpddream 2014-11-8 16:42:25 | 只看该作者
全局:
22691482 发表于 2014-11-8 15:20
Onsite据说一天就回复了。。祝好运啊!

明天是周六啊。。。多谢多谢你也好运!
回复

使用道具 举报

🔗
guomin1314 2014-11-9 08:44:13 | 只看该作者
全局:
有两个方法,不知道哪个好一些
1. 在构造函数里,获得树的最小节点。hasNext return 当前节点是否为空, next则返回当前节点并且重新计算当前节点为当前节点的successor。
2. 构造一个链表
回复

使用道具 举报

🔗
Neal_kks 2014-11-23 16:36:04 | 只看该作者
全局:
我用c++实现了一个,大家看看有么有bug吧。。
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;

  4. struct Tree{
  5.     int val;
  6.     Tree *left, *right;
  7.     Tree(int mval):val(mval), left(NULL), right(NULL){}
  8. };

  9. class TreeIterator{
  10. public:
  11.     TreeIterator(Tree *root){
  12.         flag = true;
  13.         if(root == NULL){
  14.             cur = NULL;
  15.             cout<<"invalid root point"<<endl;
  16.         }else{
  17.             while(root){
  18.                 st.push(root);
  19.                 root = root->left;
  20.             }
  21.             cur = st.top();
  22.         }
  23.     }
  24.     int value(){
  25.         if(cur == NULL){
  26.             cout<<"invalid operation"<<endl;
  27.             return -1;
  28.         }else{
  29.             cout<<cur->val<<endl;
  30.             return cur->val;
  31.         }
  32.     }
  33.     void next(){
  34.         if(cur == NULL){
  35.             cout<<"No next point"<<endl;
  36.             return;
  37.         }else{
  38.             if(cur->right){
  39.                 st.pop();
  40.                 cur = cur->right;
  41.                 st.push(cur);
  42.                 while(cur->left){
  43.                     cur = cur->left;
  44.                     st.push(cur);
  45.                 }
  46.             }else{
  47.                 st.pop();
  48.                 cur = st.empty()?NULL:st.top();
  49.             }
  50.         }
  51.     }
  52.     bool hasNext(){
  53.         return cur != NULL;
  54.     }
  55. private:
  56.     stack<Tree* > st;
  57.     Tree* cur;
  58.     bool flag;
  59. };

  60. int main(){
  61.     Tree *t1 = new Tree(1);
  62.     Tree *t2 = new Tree(2);
  63.     Tree *t3 = new Tree(3);
  64.     Tree *t4 = new Tree(4);
  65.     Tree *t5 = new Tree(5);
  66.     Tree *t6 = new Tree(6);
  67.     Tree *t7 = new Tree(7);
  68.     t1->left = t2, t1->right = t3;
  69.     t2->left = t4, t2->right = t5;
  70.     t3->left = t6, t3->right = t7;
  71.     TreeIterator tit(t1);
  72.     while(tit.hasNext()){
  73.         tit.value();
  74.         tit.next();
  75.     }
  76. }

  77. /*
  78.                 1
  79.                /\
  80.               2 3
  81.              /\ /\
  82.             4 56 7
  83.             */
复制代码
回复

使用道具 举报

🔗
pyemma 2014-11-24 01:42:39 | 只看该作者
全局:
写树的iterator,这道题我给我同学mock interview的时候就考了一下
回复

使用道具 举报

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

本版积分规则

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