Skip to content

Commit 8e8083b

Browse files
committed
Add C++ solution of problem #145
1 parent 44c0562 commit 8e8083b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

145/solution.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
vector<int> postorderTraversal(TreeNode* root) {
15+
vector<int> ans;
16+
f(root, ans);
17+
return ans;
18+
}
19+
20+
private:
21+
void f(TreeNode *root, vector<int> &ans) {
22+
if(!root)
23+
return;
24+
25+
f(root->left, ans);
26+
f(root->right, ans);
27+
ans.push_back(root->val);
28+
}
29+
};

0 commit comments

Comments
 (0)