Skip to content

Commit c6726c8

Browse files
Merge pull request #3107 from prajval-malhotra/0145-binary-tree-postorder-traversal
create: 0145-binary-tree-postorder-traversal.cpp
2 parents ef1334a + 961d2ac commit c6726c8

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Diff for: cpp/0145-binary-tree-postorder-traversal.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
if(!root) return {};
16+
17+
postorderTraversal(root->left);
18+
postorderTraversal(root->right);
19+
result.push_back(root->val);
20+
21+
return result;
22+
}
23+
private:
24+
vector<int> result;
25+
};

0 commit comments

Comments
 (0)