1. 程式人生 > >C++資料結構--二叉樹的複製和刪除

C++資料結構--二叉樹的複製和刪除



本示例所用到的二叉樹:



template<typename T>
class node
{
   public:
      T val;
//節點值
 node<T>* left; //左節點 
 node<T>* right; //右節點

 node():val(T()),left(nullptr),right(nullptr){}
 node(T v, node<T>* l=nullptr, node<T>* r=nullptr):val(v),left(l),right(r){}
};






node<char>*   createBTree()  
//構建一棵示例二叉樹 
   {
node<char>* g=new node<char>('G');
node<char>* e=new node<char>('E');
node<char>* f=new node<char>('F');
node<char>* d=new node<char>('D',nullptr,g);
node<char>* c=new node<char>('C',nullptr,f);
node<char>* b=new node<char>('B',d,e);

node<char>* a=new node<char>('A',b,c);

return a;

   }