Void allpath (bitree T, stack & S) // output the path from the root tree to all leaf nodes {If (t) {push (S, T-> data ); if (! T-> left &&! T-> right) // If the left pointer and right pointer are both empty, the node is the leaf node printstack (s); else {allpath (t-> left, s); allpath (t-> right, S);} POP (s );}}
If the left pointer of a node is null, the node is a leaf node. In fact, the right pointer is a brother node in the actual tree. It is empty, as shown in. Red indicates the actual tree, and blue indicates the binary linked list converted from the tree.
Void outpath (bitree T, stack & S) // output the path from the forest root to all leaf nodes {While (! T) {// the path from the root node to the leaf node to be output. In essence, the path from the root node to the left is appended to the leaf node. Push (S, T-> data); If (! T-> lchild) // printstack (s); else outpath (t-> lchild, S); POP (s); t = T-> rchild; // t always go to the right }}