94. 二叉树的中序遍历/C++

/**
 * 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==NULL)
            return res;
        
        stack<TreeNode*> stack;
        TreeNode* cur = root;
        while(cur!=NULL || !stack.empty()){
            while(cur!=NULL){
                stack.push(cur);
                cur=cur->left;
            }
            cur = stack.top();
            stack.pop();
            res.push_back(cur->val);
            cur=cur->right;
        }
        return res;
    }
};

相关文章:

  • 2021-07-07
  • 2021-07-27
  • 2022-01-30
  • 2022-12-23
  • 2021-07-20
  • 2021-07-23
  • 2021-08-18
  • 2021-10-13
猜你喜欢
  • 2021-12-25
  • 2022-01-17
  • 2021-07-29
  • 2021-12-18
  • 2021-08-29
  • 2021-06-26
相关资源
相似解决方案