###### 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); } } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up