The linked list storage structure of a known binary tree is defined as follows:
Typedef struct bintnode
{
Int data;
Bintnode * lchild;
Bintnode * rchild;
Bintnode (INT elemet = 0, bintnode * Left = NULL, bintnode * Right = NULL)
: Data (elemet), lchild (left), rchild (right ){}
} Bintnode;
Write a recursive algorithm that uses the left-right link of the leaf node to link all leaf nodes from left to right into a single-chain table, the algorithm returns the address (chain header) of the leftmost leaf node ).
Algorithm Analysis: You can traverse the binary tree in the First Order, "Left and Right "!
# Include "stdafx. H "# include <iostream> using namespace STD; typedef struct bintnode {int data; bintnode * lchild; bintnode * rchild; bintnode (INT elemet = 0, bintnode * Left = NULL, bintnode * Right = NULL): Data (elemet), lchild (left), rchild (right) {}} bintnode; // recursively create a binary tree bintnode * createbintree (INT arr [], int begin, int end) {bintnode * root = NULL; If (begin> = END) return root; root = new bintnode (ARR [begin]); root -> Lchild = createbintree (ARR, begin * 2 + 1, end); root-> rchild = createbintree (ARR, begin * 2 + 2, end); Return root ;} // function: recursive. Use the left-right pointer field rchild in the leaf node to link all leaf nodes from left to right into a single-chain table, return the leftmost leaf node address bintnode * head = NULL; bintnode * temp = NULL; void changletosinglelist (bintnode * root) {If (null = root) // The tree is empty return; // The leaf node if (root-> lchild = NULL & root-> rchild = NULL) {If (null = temp) {temp = head = root;} else {temp-> r Child = root; temp = root; }}changletosinglelist (root-> lchild); // left subtree changletosinglelist (root-> rchild); // right subtree} int main () {const int n = 10; int arr [N]; for (INT I = 0; I <n; I ++) Arr [I] = I + 1; bintnode * root = createbintree (ARR, 0, n); changletosinglelist (Root); While (Head! = NULL) {cout