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

热乎乎的fb店面

🔗
 楼主| cocaptainco 2015-11-4 23:23:01 | 只看该作者
全局:
akluffy 发表于 2015-11-4 22:33
如果默认一定找到target,那直接在树里找到target的node,那么

仅仅小于target的node的值一定是findLa ...

因为不一定有left child 和 right child
回复

使用道具 举报

🔗
akluffy 2015-11-5 07:31:31 | 只看该作者
全局:
这题说简单,简单,没有复杂的算法数据机构,说难也有点难,代码很多。
我把我写的发一下,test case跑了下,没有问题。
  1. class Node{
  2. public:
  3.     int val;
  4.     Node *left, *right;
  5.     Node(int val) {
  6.         this->val = val;
  7.         left = NULL;
  8.         right = NULL;
  9.     }
  10. };

  11. Node* findMinNode(Node *root) {
  12.     if(!root) return NULL;
  13.     while(root->left) root = root->left;
  14.     return root;
  15. }

  16. Node* findMaxNode(Node *root) {
  17.     if(!root) return NULL;
  18.     while(root->right) root = root->right;
  19.     return root;
  20. }

  21. void findNumbers(Node *root, int target, Node *&minNode, Node *&maxNode) {
  22.     if(!root) return;
  23.     if(root->val < target) {
  24.         minNode = root;
  25.         findNumbers(root->right, target, minNode, maxNode);
  26.     } else if(root->val > target) {
  27.         maxNode = root;
  28.         findNumbers(root->left, target, minNode, maxNode);
  29.     } else if(root->val == target) {
  30.         if(root->left) minNode = findMaxNode(root->left);
  31.         if(root->right) maxNode = findMinNode(root->right);
  32.     }
  33. }

  34. void inordrerPrint(Node *root) {
  35.     if(!root) return;
  36.     if(root->left) inordrerPrint(root->left);
  37.     cout << root->val << ", ";
  38.     if(root->right) inordrerPrint(root->right);
  39. }

  40. int main(char **argv, int argc)
  41. {
  42.     Node *root = new Node(10);
  43.     root->left = new Node(5);
  44.     root->left->left = new Node(3);
  45.     root->left->right = new Node(8);

  46.     root->right = new Node(15);
  47.     root->right->left = new Node(12);
  48.     root->right->left->left = new Node(11);
  49.     root->right->left->right = new Node(14);
  50.     root->right->right = new Node(18);
  51.     root->right->right->left = new Node(16);
  52.     root->right->right->right = new Node(19);

  53. inordrerPrint(root);
  54. cout << endl;

  55.     Node *prev = NULL, *next = NULL;
  56.     for(int i = 4; i <= 18; ++i) {
  57.         int target = i;
  58.         findNumbers(root, target, prev, next);
  59.         cout << "prev : " << prev->val << "  target : " << target << "  next : " << next->val << endl;
  60.     }

  61.     return 0;
  62. }
复制代码
回复

使用道具 举报

🔗
 楼主| cocaptainco 2015-11-5 08:18:25 | 只看该作者
全局:
akluffy 发表于 2015-11-5 07:31
这题说简单,简单,没有复杂的算法数据机构,说难也有点难,代码很多。
我把我写的发一下,test case跑了 ...

也对,predecessor 和 successor of BST node, 网上有这个代码。。。可惜之前没看到
回复

使用道具 举报

🔗
akluffy 2015-11-5 13:07:10 | 只看该作者
全局:
cocaptainco 发表于 2015-11-5 08:18
也对,predecessor 和 successor of BST node, 网上有这个代码。。。可惜之前没看到

嗯面试还是很看运气的。
回复

使用道具 举报

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

本版积分规则

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