Given a binary tree, return the preorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

 

return [1,2,3].

 

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<int> preorderTraversal(TreeNode* root) {
13         vector<int> res;
14         help(root,res);
15         return res;
16     }
17     void help(TreeNode *root,vector<int> &res)
18     {
19         if(root!=NULL)
20         {
21             res.push_back(root->val);
22             help(root->left,res);
23             help(root->right,res);
24             
25         }
26     }
27 };
28 class Solution {
29 public:
30     vector<int> preorderTraversal(TreeNode *root)
31     {
32         vector<int> res;
33         stack<TreeNode* > s;
34         s.push(root);
35         while(!s.empty())
36         {
37             TreeNode *now=s.top();
38             s.pop();
39             if(now!=NULL)
40             {
41                 res.push_back(now->val);
42                 s.push(now->right);
43                 s.push(now->left);
44             }
45         }    
46         return res;
47     }
48     
49 };