vector可以替代stack,stack仅支持一端操作(push,pop),而vector除此之外(push_back,pop_back)还支持中间插入(insert)、‘移除(erase),所以要用vector替代stack如有:/*struct TreeNode { int val; TreeNode *left; TreeNode *right;};*/stack<TreeNode*> s;s.push(root);while(!s.empty()) { TreeNode *tmp = s.top(); if(tmp != NULL) { cout << tmp->val; // visit node. s.push(tmp->right); s.push(tmp->left); }}想直接替换可以改为:vector<TreeNode*> s;s.push_back(root);while(!s.empty()) { TreeNode *tmp = s.back(); if(tmp != NULL) { cout << tmp->val; // visit node. s.push_back(tmp->right); s.push_back(tmp->left); }}在STL中stack本身就是一个容器适配器,默认情况是以deque实现:template < class T, class Container = deque<T> > class stack;这里的Container必须supports the following operations:front()back()push_back()pop_back()而vector就满足这些条件,所以要用vector替代deque作为stack的底层容器,只需这样定义即可:stack<TreeNode*, vector<TreeNode*> > stk;