二叉树的中序遍历

class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        std::stack<TreeNode *> stack;
        std::vector<int> res;
        while (root != nullptr || !stack.empty()) {
            while (root != nullptr){
                stack.push(root);
                root = root->left;
            }
            root = stack.top();
            stack.pop();
            res.push_back(root->val);
            root = root->right;
        }
        return res;
    }
};

 

posted on 2021-05-20 19:51  QzZq  阅读(43)  评论(0)    收藏  举报

导航