题解 | #牛牛的二叉树后序遍历#
牛牛的二叉树后序遍历
https://www.nowcoder.com/practice/2f5daf5a5ed34572860266462ba21cad
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
vector<int> ans;
//递归法
void LRD(TreeNode* cur){
if(!cur) return;
LRD(cur->left);
LRD(cur->right);
ans.push_back(cur->val);
}
vector<int> postorderTraversal(TreeNode* root) {
LRD(root);
return ans;
}
};

查看18道真题和解析