The most basic algorithm Exercise 2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.