/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
if(!root){
return res;
}
stack<TreeNode*> stk;
TreeNode* node=root;
while(!stk.empty() || node!=nullptr){
while(node){
stk.emplace(node);
node=node->left;
}
node=stk.top();
stk.pop();
res.emplace_back(node->val);
node=node->right;
}
return res;
}
};
二叉树的中序遍历
最新推荐文章于 2024-11-14 13:49:39 发布