# 【LeetCode】 145. Binary Tree Postorder Traversal ## Description > Given a binary tree, return the postorder traversal of its nodes' values. > 給一棵二元樹,回傳這棵樹的後序遍歷。 ## Example: ``` Input: [1,null,2,3] 1 \ 2 / 3 Output: [3,2,1] ``` ## Solution * 二元樹中的基本題。使用遞迴跑每個節點,有左邊就往左邊走,然後往右邊走,最後加入自己。 * 如果想得到前序或中序,只需要改變下方code中Line`16`~`18`的順序即可。 * [94. Binary Tree Inorder Traversal](https://hackmd.io/s/rJeTH3bT4) * [144. Binary Tree Preorder Traversal](https://hackmd.io/s/H1wDu2-p4) ### Code ```C++=1 /** * 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: void GetPostorder(TreeNode* root, vector<int>& v) { if(root) { GetPostorder(root->left,v); GetPostorder(root->right,v); v.push_back(root->val); } } vector<int> postorderTraversal(TreeNode* root) { vector<int> v; GetPostorder(root, v); return v; } }; ``` ###### tags: `LeetCode` `C++`