Easy C++ solution using Stack
class Solution {\n    public:\n    vector<int> preorderTraversal(TreeNode *root) {\n        if (root==NULL) {\n            return vector<int>();\n        }\n   
    class Solution {\n    public:\n    vector<int> preorderTraversal(TreeNode *root) {\n        if (root==NULL) {\n            return vector<int>();\n        }\n        vector<int> result;\n        stack<TreeNode *> treeStack;\n        treeStack.push(root);\n        while (!treeStack.empty()) {\n            TreeNode *temp = treeStack.top();\n            result.push_back(temp->val);\n            treeStack.pop();\n            if (temp->right!=NULL) {\n                treeStack.push(temp->right);\n            }\n            if (temp->left!=NULL) {\n                treeStack.push(temp->left);\n            }\n        }\n        return result;\n    }\n    };
[]