/**
* Definition for binary tree
* 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;
if(root) {
preorder(res,root);
}
return res;
}
void preorder(vector<int> &res, TreeNode *root) {
res.push_back(root->val);
if(root->left) {
preorder(res,root->left);
}
if(root->right) {
preorder(res,root->right);
}
}
};
[leetcode]Binary Tree Preorder Traversal
最新推荐文章于 2022-07-19 12:03:10 发布