### 將二叉樹葉子節點用rchild鏈成一個單鏈表

#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;

//遞歸創建二叉樹

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;

//函數功能：遞歸，利用葉子結點中空的右指針域rchild，

//將所有葉子結點自左向右連接成一個單鏈表，返回最左葉子結點的地址

BinTNode *temp = NULL;

void ChangleToSingleList(BinTNode *root)

if (NULL == root)//樹為空

return

//葉子結點

if (root->lchild == NULL && root->rchild == NULL)

{

if(NULL == temp)

{

}

else

{

temp->rchild = root;

temp = root;

}

}

ChangleToSingleList(root->lchild);//左子樹

ChangleToSingleList(root->rchild);//右子樹

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);

{

}

cout<<endl;