algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

commit 657898d3beb19a305fb48a90cd8a637a7291975d
parent 91519e362bc788726d612a54f7581c23b0db711c
Author: Andrew Laack <andrew@laack.co>
Date:   Sat, 22 Aug 2026 14:14:22 -0500

Binary tree ordering

Diffstat:
Abinary-tree-inorder/binary-tree-traversal-iterative.cpp | 48++++++++++++++++++++++++++++++++++++++++++++++++
Abinary-tree-inorder/binary-tree-traversal.cpp | 31+++++++++++++++++++++++++++++++
Abinary-tree-postorder-traversal/postorder-traversal.cpp | 32++++++++++++++++++++++++++++++++
Abinary-tree-preeorder-traversal/preorder-traversal-iterative.cpp | 38++++++++++++++++++++++++++++++++++++++
Abinary-tree-preeorder-traversal/preorder-traversal.cpp | 37+++++++++++++++++++++++++++++++++++++
5 files changed, 186 insertions(+), 0 deletions(-)

diff --git a/binary-tree-inorder/binary-tree-traversal-iterative.cpp b/binary-tree-inorder/binary-tree-traversal-iterative.cpp @@ -0,0 +1,48 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + vector<int> inorderTraversal(TreeNode* root) { + + auto* start = root; + + // left, current, right + auto order = vector<int>{}; + auto stack = vector<TreeNode*>{root}; + + while(stack.size() > size_t(0)) { + auto current = stack.back(); + stack.pop_back(); + + if(current == nullptr) { + continue; + } + + if(current->left == nullptr && current->right == nullptr) { + order.push_back(current->val); + continue; + } + + stack.push_back(current->right); + current->right = nullptr; + stack.push_back(current); + stack.push_back(current->left); + current->left = nullptr; + } + + // reset to point at root at the end so + // no side effects for this specific function. + + root = start; + return order; + } +}; diff --git a/binary-tree-inorder/binary-tree-traversal.cpp b/binary-tree-inorder/binary-tree-traversal.cpp @@ -0,0 +1,31 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void recurse(TreeNode* current, vector<int>& order) { + if(current == nullptr) { + return; + } + recurse(current->left, order); + order.push_back(current->val); + recurse(current->right, order); + return; + } + + vector<int> inorderTraversal(TreeNode* root) { + // left, current, right + auto order = vector<int>{}; + recurse(root, order); + return order; + } +}; diff --git a/binary-tree-postorder-traversal/postorder-traversal.cpp b/binary-tree-postorder-traversal/postorder-traversal.cpp @@ -0,0 +1,32 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + vector<int> postorderTraversal(TreeNode* root) { + auto result = vector<int>{}; + recurse(root,result); + return result; + } +private: + void recurse(TreeNode* current, vector<int>& result) { + if(current == nullptr) { + return; + } + + recurse(current->left, result); + recurse(current->right, result); + result.push_back(current->val); + + return; + } + +}; diff --git a/binary-tree-preeorder-traversal/preorder-traversal-iterative.cpp b/binary-tree-preeorder-traversal/preorder-traversal-iterative.cpp @@ -0,0 +1,38 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + vector<int> preorderTraversal(TreeNode* root) { + + auto nodeLs = vector<TreeNode*>{root}; + auto returnLs = vector<int>{}; + + while (nodeLs.size() > size_t(0)) { + auto* current = nodeLs.back(); + nodeLs.pop_back(); + + if(current == nullptr) { + continue; + } + + returnLs.push_back(current->val); + + // preorder so we add current then traverse left + // then traverse right. (NOTE: Remember this is stack so last item is popped) + + nodeLs.push_back(current->right); + nodeLs.push_back(current->left); + } + + return returnLs; + } +}; diff --git a/binary-tree-preeorder-traversal/preorder-traversal.cpp b/binary-tree-preeorder-traversal/preorder-traversal.cpp @@ -0,0 +1,37 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void recurse (TreeNode* current, vector<int>& accumulator) { + if (current == nullptr) { + return; + } + + accumulator.push_back(current->val); + + auto* left = current->left; + auto* right = current->right; + + recurse(left,accumulator); + recurse(right,accumulator); + + return; + + } + vector<int> preorderTraversal(TreeNode* root) { + + auto result = vector<int>{}; + recurse(root, result); + return result; + } +};