/**
* 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> res;
if(!root)
return res;
stack<TreeNode*> stack;
TreeNode* cur = root;
while(cur || !stack.empty()){
while(cur){
stack.push(cur);
cur=cur->left;
}
cur = stack.top();
stack.pop();
res.push_back(cur->val);
cur=cur->right;
}
return res;
}
};
94. 二叉树的中序遍历/C++
树的各种遍历及C++代码总结
最新推荐文章于 2024-09-29 17:52:02 发布
博客主要总结了树的各种遍历方式,并给出了对应的C++代码,涉及信息技术领域的数据结构相关知识。

1245

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



