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;
}
}
}

本文提供了一个微软面试题目示例代码,通过伪代码形式展示如何遍历二叉树并根据特定条件打印节点值。代码使用了栈来辅助实现非递归的中序遍历。
1437

被折叠的 条评论
为什么被折叠?



