Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created January 21, 2019 00:42
Embed
What would you like to do?
/**
* 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> res;
rec(res, root);
return res;
}
void rec(vector<int> &res, TreeNode* root){
if(root == NULL) return;
//We first record the root.
res.push_back(root->val);
rec(res, root->left);
rec(res, root->right);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment