mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-17 16:51:46 +00:00
Initial commit
This commit is contained in:
31
Easy/binary-tree-postorder-traversal/solution.cpp
Normal file
31
Easy/binary-tree-postorder-traversal/solution.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <vector>
|
||||
|
||||
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 {
|
||||
private:
|
||||
void traverse(std::vector<int> &list, TreeNode *root) {
|
||||
if (root == nullptr) {
|
||||
return;
|
||||
} else {
|
||||
traverse(list, root->left);
|
||||
traverse(list, root->right);
|
||||
list.push_back(root->val);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
std::vector<int> postorderTraversal(TreeNode *root) {
|
||||
std::vector<int> list;
|
||||
traverse(list, root);
|
||||
return list;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user