3 Approaches in C++ || Recursive, Iterative, Morris Traversal
Recursive Approach:\n\nvoid help(TreeNode* root, vector<int>& ans)\n    {\n        if(root == NULL)\n            return;\n        ans.push_back(root->val);\n   
**Recursive Approach:**\n````\nvoid help(TreeNode* root, vector<int>& ans)\n    {\n        if(root == NULL)\n            return;\n        ans.push_back(root->val);\n        help(root->left, ans);\n        help(root->right, ans);\n    }\n    vector<int> preorderTraversal(TreeNode* root) {\n        vector<int> ans;\n        help(root, ans);\n        return ans;\n    }\n````\n\n-------------------------------------------------------------------------------------------------------\n**Iterative Approach:**\n````\nvector<int> preorderTraversal(TreeNode* root) {\n        vector<int> ans;\n        if(root==NULL) return ans;\n        stack<TreeNode*> st;\n        st.push(root);\n        while(!st.empty())\n        {\n            root = st.top();\n            st.pop();\n            ans.push_back(root->val);\n            if(root->right!=NULL)\n            {\n                st.push(root->right);\n            }\n            if(root->left!=NULL)\n            {\n                st.push(root->left);\n            }\n        }\n        return ans;\n    }\n````\n\n-------------------------------------------------------------------------------------------------------\n**Morris Traversal :**\n````\nvector<int> preorderTraversal(TreeNode* root) {\n        vector<int> ans;\n        TreeNode *node = root;\n        while(node != NULL)\n        {\n            if(node->left == NULL) \n            {\n                ans.push_back(node->val);\n                node = node->right;\n            }\n            else\n            {\n                TreeNode *prev = node->left;\n                while(prev->right && prev->right != node)\n                {\n                    prev = prev->right;\n                }\n                if(prev->right == NULL)\n                {\n                    prev->right = node;\n                    ans.push_back(node->val);\n                    node = node->left;\n                }\n                else\n                {\n                    prev->right = NULL;\n                    node = node->right;\n                }\n            }\n        }\n        return ans;\n    }\n````\n\n**One Request : Please do upvote if you found these solutions helpful. Thanks in advance!**
[]