Print nodes in a given range of BST

来源:互联网 发布:英雄联盟录制软件 编辑:程序博客网 时间:2024/05/30 23:01

I am stupid..... this is a MS interview question.

Pseudo code

void printNode(root) { if(!root) return; stack<Node*> nodes; Node* pCurrent; while(!pCurrent || !nodes.empty()) {    if(pCurrent) {      nodes.push(pCurrent);      Node* tmp = nodes.top();      if(tmp->val < small) {root = NULL;}      else {        pCurrent = pCurrent->left;      }    } else {      Node* top = stack.top();      stack.pop();      if(top->val >= small && top->val <= big) cout << top->val << endl;      if(top->val > big) pCurrent = NULL;     else pCurrent = top->right;    }  }}

0 0
原创粉丝点击