/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> v;
preOrder(v, root);
return v;
}
private:
void preOrder(vector<int>& v, TreeNode* root) {
if (!root) {
return;
}
v.push_back(root->val);
preOrder(v, root->left);
preOrder(v, root->right);
}
};