###### tags: `LeetCode` `Recursion` `Postorder` `Tree` `Easy` # LeetCode #145 [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) ### (Easy) 給定一個二元樹,返回它的後序遍歷。 --- 與#94類似, 只不過postorder的拜訪順序為左子樹->右子樹->root。 --- ``` class Solution { public: vector<int> postorderTraversal(TreeNode* root) { if(root){ vector<int> nodes; helper(root, nodes); return nodes; } return {}; } void helper(TreeNode* root, vector<int>& nodes){ if(root){ helper(root->left, nodes); helper(root->right, nodes); nodes.push_back(root->val); } } }; ```
×
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