Restore binary Tree
1#include <fstream>2#include <iostream>3 4 using namespacestd;5 6 structnode7 {8 CharC;9 structNode *lch,*rch;Ten }; One A intCount_leaf (node* tree);//Statistical leaf node - intCount_deep (node* tree);//depth of the statistical tree - voidCreate_tree (CharLChar* R,intIintJintEintf,node** tree);//restoration of binary tree by sequence traversal sequences and sequence traversal sequences the - intMain () - { - //freopen ("d:\\input.in", "R", stdin); + //freopen ("D:\\output.out", "w", stdout); -Node *tree;cout<<int(tree) <<endl<<int(&tree) <<Endl; + Charpre[]="ABDGCEF"; A Char inch[]="DGBAECF"; atCreate_tree (PRE,inch,0,6,0,6,&tree); -Cout<<count_leaf (tree) <<Endl; -Cout<<count_deep (tree) <<Endl; - return 0; - } - intCount_leaf (node*tree) in { - if(Tree==null)return 0; to if(Tree->lch==null&&tree->rch==null)return 1; + returnCount_leaf (Tree->lch) +count_leaf (tree->rch); - } the intCount_deep (node*tree) * { $ if(Tree==null)return 0;Panax Notoginseng return 1+max (Count_deep (Tree->lch), Count_deep (tree->rch)); - } the voidCreate_tree (CharLChar* R,intIintJintEintf,node**tree) + { A intm; the(*tree) =Newnode; +(*tree)->c=L[i]; -m=e; $ while(R[m]!=l[i]) m++; $ if(m==e) (*tree)->lch=NULL; - ElseCreate_tree (l,r,i+1, i+m-e,e,m-1,& (*tree)LCH); - if(m==f) (*tree)->rch=NULL; the ElseCreate_tree (l,r,i+m-e+1, j,m+1,f,& (*tree)rch); -}View Code
The most basic algorithm Exercise 2