From 3f08888cf4c6324c0c86be1ce1b3383d0396a1c4 Mon Sep 17 00:00:00 2001 From: srish4884 <115385418+srish4884@users.noreply.github.com> Date: Sun, 9 Oct 2022 11:59:07 +0530 Subject: [PATCH] Added Binary_Tree_Postorder_Traversal.cpp --- .../Binary_Tree_Postorder_Traversal.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Binary search tree/Binary_Tree_Postorder_Traversal.cpp 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); + } +};