/**
* 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++
最新推荐文章于 2022-05-29 00:02:13 发布