---
# System prepended metadata

title: 'LeetCode #144 [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)'
tags: [Recursion, Preorder, LeetCode, Easy, Tree]

---

###### tags: `LeetCode` `Easy` `Preorder` `Recursion` `Tree`
# LeetCode #144 [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)
### (Easy)
給你二元樹的根節點 root ，返回它節點值的前序遍歷。


---
與 #94 相似, 不過preorder的順序改為root->右子樹->左子樹。


---
```
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        if(root){
            vector<int> nodes;
            helper(root, nodes);
            return nodes;
        }
        return {};
    }
    void helper(TreeNode* root, vector<int>& nodes){
        if(root){
            nodes.push_back(root->val);
            helper(root->left, nodes);
            helper(root->right, nodes);
        }
    }
};
```