给定一个二叉树,返回它的 后序 遍历。
示例:
输入: [1,null,2,3]
1
2
/
3
输出: [3,2,1]
思路:
后序遍历
AC代码:(C++)
class Solution {
public:
void postorder(TreeNode* root, vector& res) {
if (root == nullptr) {
return;
}
postorder(root->left, res);
postorder(root->right, res);
res.push_back(root->val);
}
vector postorderTraversal(TreeNode* root) {
vector res;
postorder(root, res);
return res;
}
};


![[leetcode]145.二叉树的后序遍历 [leetcode]145.二叉树的后序遍历](http://www.mshxw.com/aiimages/31/290121.png)
