-
Notifications
You must be signed in to change notification settings - Fork 6
/
0144.cpp
36 lines (28 loc) · 867 Bytes
/
0144.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void pre_order(TreeNode* root, vector<int>& values){
if(!root)
return;
values.push_back(root->val);
if(root->left)
pre_order(root->left, values);
if(root->right)
pre_order(root->right, values);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
pre_order(root, result);
return result;
}
};