/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void tar(TreeNode *cur,vector<int> &vec){
if (cur == NULL) return;
tar(cur->left,vec);
tar(cur->right,vec);
vec.push_back(cur->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
tar(root,res);
return res;
}
};
145. 二叉树的后序遍历
最新推荐文章于 2024-08-29 22:25:44 发布