diff --git a/Binary search tree/Binary_Tree_Postorder_Traversal.cpp b/Binary search tree/Binary_Tree_Postorder_Traversal.cpp new file mode 100644 index 0000000..108d0d9 --- /dev/null +++ b/Binary search tree/Binary_Tree_Postorder_Traversal.cpp @@ -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 postorderTraversal(TreeNode* root) { + vector nodes; + postorder(root, nodes); + return nodes; + } +private: + void postorder(TreeNode* root, vector& nodes) { + if (!root) { + return; + } + postorder(root -> left, nodes); + postorder(root -> right, nodes); + nodes.push_back(root -> val); + } +};