✅striver's method  || Upvote if you like ⬆️⬆️
\n\n# Code\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNod
\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    vector<int> preorderTraversal(TreeNode* root) {\n        vector<int> preorder;\n        if(root==NULL) return preorder;\n        stack<TreeNode*> st;\n        st.push(root);\n        while(!st.empty())\n        {\n\n           root=st.top();\n           st.pop();\n           preorder.push_back(root->val);\n           if(root->right!=NULL){\n               st.push(root->right);\n           }\n\n           if(root->left!=NULL)\n           {\n               st.push(root->left);\n           }\n\n        }\n        return preorder;\n        \n    }\n};\n```
['C++']