My simple c++ 2 solutions


  • 2
    K
    class Solution {
    public:
    
    void helper(TreeNode * root, vector<int>&res)
    {
        if(!root)return;
        helper(root->left, res);
        res.push_back(root->val);
        helper(root->right, res);
    }
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int>res;
        if(!root)return res;
        helper(root, res);
        return res;
    }
    

    };

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

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.