二叉树——145. 二叉树的后序遍历
二叉树——145. 二叉树的后序遍历
题目:

思路:
迭代,顺序是左右中
代码:
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
// 放结果
vector<int> res;
postorder(root, res);
return res;
}
void postorder(TreeNode* root, vector<int> &res){
if(!root) return;
postorder(root->left, res); // 左
postorder(root->right, res); // 右
res.push_back(root->val); // 中
}
};
Rank:


浙公网安备 33010602011771号