您的位置:首页 > 其它

【二叉树5】前中后遍历二叉树的递归和非递归方法

2013-09-09 12:15 169 查看
/article/4719934.html
【问题】非递归中序遍历

【code】

为什么局部定义的vector可以作为返回?

vector<int> inorderTraversal(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> output;
stack<TreeNode *> st;
while (!st.empty() || root != NULL) {
while (root != NULL) {
st.push(root);
root = root->left;
}

if (!st.empty()) {
root = st.top();
output.push_back(root->val);
st.pop();
root = root->right;
}
}
return output;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: