Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Binary search tree/Binary_Tree_Postorder_Traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Approach #1: Recursive Method

The recursive Approach is the straight forward and classical approach.

If the current node is not empty :
1. Traverse the left subtree
2. Traverse the right subtree
3. Print the node

class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> nodes;
postorder(root, nodes);
return nodes;
}
private:
void postorder(TreeNode* root, vector<int>& nodes) {
if (!root) {
return;
}
postorder(root -> left, nodes);
postorder(root -> right, nodes);
nodes.push_back(root -> val);
}
};