查看: 2370| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

Amazon : implement binary tree leaf iterator

全局:

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

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

x
Implement Iterator on Binary Tree that iterates through leaf nodes left to right

上一篇:Google : Find the number of duplicated number in sorted array
下一篇:Microsoft : 找出唯一重复的数
🔗
darksteel 2011-7-29 15:12:48 | 只看该作者
全局:
  1. template <typename T>
  2. class Iterator
  3. {
  4.         public:
  5.         Iterator(T *root)
  6.         {
  7.                 if(root == NULL) return;
  8.                 st.push(root);
  9.                 probe();
  10.         }
  11.         bool hasNext() { return (st.size() > 0); }
  12.         T *next()
  13.         {
  14.                 T *node = st.top();
  15.                 st.pop();
  16.                 if(!st.empty())
  17.                         probe();
  18.                 return node;
  19.         }
  20.         private:
  21.         stack<T *> st;
  22.         bool leaf(T *node)
  23.         {
  24.                 return (node->leftChild() == NULL && node->rightChild() == NULL);
  25.         }
  26.         void probe()
  27.         {
  28.                 T *now = st.top();
  29.                 while(!leaf(now)) {
  30.                         st.pop();
  31.                         if(now->rightChild())
  32.                                 st.push(now->rightChild());
  33.                         if(now->leftChild())
  34.                                 st.push(now->leftChild());
  35.                         now = st.top();
  36.                 }
  37.         }
  38. };
复制代码
类似java中iterator的实现,tree node类必须有leftChild和rightChild方法。传入root指针,iterate所有叶节点的指针
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-7-30 09:04:09 | 只看该作者
全局:
下面是我的函数版,不要额外空间但是时间复杂度更大,类似于STL里对二叉树的iterator方法:

  1. struct NODE
  2. {
  3.         int nVal;
  4.         NODE* pLft;
  5.         NODE* pRgt;

  6.         NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL)
  7.         {}
  8. };

  9. void _inner_get_next(NODE* pNode, NODE* pIter, bool& bFlag, NODE*& pNext)
  10. {
  11.         if (NULL == pNode) return;
  12.         _inner_get_next(pNode->pLft, pIter, bFlag, pNext);

  13.         if (NULL != pNext) return;
  14.         if (bFlag && NULL == pNode->pLft && NULL == pNode->pRgt)
  15.         {
  16.                 pNext = pNode;
  17.                 return;
  18.         }

  19.         if (pIter == pNode) bFlag = true;

  20.         _inner_get_next(pNode->pRgt, pIter, bFlag, pNext);
  21. }

  22. NODE* GetNext(NODE* pRoot, NODE* pIter)
  23. {
  24.         if (NULL == pRoot || NULL == pIter)
  25.                 return NULL;

  26.         NODE* pNext = NULL;
  27.         bool bFlag = false;
  28.         _inner_get_next(pRoot, pIter, bFlag, pNext);

  29.         return pNext;
  30. }
复制代码
回复

使用道具 举报

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

本版积分规则

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