您的位置:首页 > Web前端 > Node.js

Print nodes in a given range of BST

2016-06-07 06:17 316 查看
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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: