Last time, we looked at a way to delete a binary tree non-recursively, assuming that we had parent pointers. But nodes in a classical binary tree does not remember their parents, so we'll have to use some other trick. Recursion is the traditional mechanism for remembering the parent of the node under study, but our goal here is to do it without recursion.The trick is to realize that each child pointer in the binary tree is traversed only once. Therefore, once you have traversed the pointer, it is superfluous and can be reused. Since the left child is traversed first, we can repurpose it to be the parent pointer.struct Node{ Node* left; Node* right; Data d;};void DeleteTree(Node* node){ Node* parent = nullptr; while (node) { // Go left as far as you can. while (node->left) { // Move down and remember the old parent in the "left" pointer. node = std::exchange(node->left, std::exchange(parent, node)); } // Remember the parent in the "left" pointer. node->left = parent; // If you can't go left, try going right one step, // and then continue the leftward descent. if (node->right) { parent = node; node = node->right; continue; } // At the bottom. Delete the node and head back up. while (node) { delete node; if (!parent) { return; // all done } // If we came from the left child, // then go to the right child if there is one. if (?????????????????????????????????????) { node = parent->right; break; } else { // No more children, so keep walking up. node = parent; parent = node->left; } } }}We solved the problem of keeping track of where the parent node is, but we created a new one: When we move up the tree, how do we know whether we came up from the left child or up from the right child?Well, we know that the right child is undamaged, so instead of saying "If we are not the left child, then we are the right child", we can test the other child: "If we are not the right child, then we are the left child." if (node != parent->right && parent->right) Commenter Sebastian Redl noted that comparing pointers to deleted objects is undefined behavior, so we'll have to tweak the code. // At the bottom. Delete the node and head back up. while (node) { bool from_left = node != parent->right; delete node; if (!parent) { return; // all done } // If we came from the left child, // then go to the right child if there is one. if (from_left && parent->right) { node = parent->right; break; } else { // No more children, so keep walking up. node = parent; parent = node->left; } }This code is tricky to write because you have to keep track of the point at which the left changes to parent for each node in the tree. The solution on the Code Golf Stack Exchange is a little different: It uses the right pointer to record the "child node to process next", so it is the original right when the code traverses into the left child, and it turns to null when the code traverses into the right child. The details are different but the basic idea is the same.As I noted last time, this was not the algorithm I came up with when I was presented with this challenge. We'll look at that algorithm next time. (Spoiler alert: I like my algorithm better.)