类似前序和中序,只是读取节点值的顺序不同而已
//Runtime: 0 ms, faster than 100.00%
#include <ios>
static auto fastInput = []() {
ios_base::sync_with_stdio(false),cin.tie(nullptr);
return 0;
}();
class Solution {
public:
vector<int> array;
vector<int> postorderTraversal(TreeNode* root) {
travelTree(root);
return array;
}
void travelTree(TreeNode* root){
if(root){
travelTree(root->left);
travelTree(root->right);
array.push_back(root->val);
}
}
};