二叉树中序遍历(非递归)
程序员文章站
2022-06-17 17:36:20
...
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> re;
stack<TreeNode*> S;
TreeNode* p=root;
while(p||!S.empty()){
if(p){
S.push(p);
p=p->left;
}else{
p=S.top();
S.pop();
re.emplace_back(p->val);
p=p->right;
}
}
return re;
}
};
下一篇: access转sql工具