leetcode - Binary Tree Preorder Traversal
2013-11-10 10:58 张汉生 阅读(149) 评论(0) 编辑 收藏 举报
1 /** 2 * Definition for binary tree 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 void preOr(TreeNode * node, vector<int> & rlt){ 13 if (node==NULL) 14 return; 15 rlt.push_back(node->val); 16 preOr(node->left,rlt); 17 preOr(node->right,rlt); 18 } 19 vector<int> preorderTraversal(TreeNode *root) { 20 // IMPORTANT: Please reset any member data you declared, as 21 // the same Solution instance will be reused for each test case. 22 vector<int> rlt; 23 preOr(root, rlt); 24 return rlt; 25 } 26 };