Topic
Enter a binary tree for the pre-order traversal and the middle sequence traversal, please reconstruct the two-fork tree. It is assumed that no duplicate numbers are included in the results of the input's pre-order traversal and the middle-order traversal.
For example, a pre-sequence traversal sequence: {1,2,3,7,3,5,6,8}, the middle sequence traversal sequence: {4,7,2,1,5,3,8,6}
Answer
Pre-order Traversal:
The pre-order traversal first accesses the root node and then traverses the left subtree, and finally traverses the right subtree. While traversing the left and right subtrees, the root node is still accessed first, then the left subtree is traversed, and the right subtree is traversed at last.
Middle Sequence Traversal:
The middle sequence traversal first traverses the left subtree, then accesses the root node and finally traverses the right sub-tree. While traversing the left and right subtrees, the left subtree is still traversed, the root node is accessed, and the right subtree is traversed at last.
1#include"stdafx.h"2#include <deque>3#include <iostream>4 #defineTreelen 65 6 structBinarytreenode7 {8 intM_nvalue;9binarytreenode*M_pleft;Tenbinarytreenode*M_pright; One }; A - voidPrinttreenode (binarytreenode*Pnode) - { the if(Pnode! =NULL) - { -printf"value of this node is:%c\n", pnode->m_nvalue); - + if(Pnode->m_pleft! =NULL) -printf"Value of its left child is :%c.\n", pnode->m_pleft->m_nvalue); + Else Aprintf"Left null.\n"); at - if(Pnode->m_pright! =NULL) -printf"Value of its right child is :%c\n", pnode->m_pright->m_nvalue); - Else -printf"Right null\n"); - } inprintf"\ n"); - } to + voidPrinttree (binarytreenode*proot) - { the Printtreenode (proot); * $ if(Proot! =NULL)Panax Notoginseng { - if(Proot->m_pleft! =NULL) thePrinttree (proot->m_pleft); + A if(Proot->m_pright! =NULL) thePrinttree (proot->m_pright); + } - } $ $binarytreenode* Rebuild (Char*preorder,Char* Inorder,intlength) - { - if(preorder = = NULL | | inorder = NULL | | length <=0) the returnNULL; - Wuyi Charc = preorder[0]; the -binarytreenode* root =NewBinarytreenode (); WuRoot->m_nvalue =C; -Root->m_pright = Root->m_pleft =NULL; About $ inti; - for(i =0; I < length && inorder[i]! = C; i++); - intLeftlength =i; - intRightlength = Length-i-1; A + if(Leftlength >0) theRoot->m_pleft = Rebuild (&preorder[1],&inorder[0],leftlength); - $ if(rightlength>0) theRoot->m_pright = Rebuild (&preorder[leftlength +1], &inorder[leftlength+1], rightlength); the returnRoot; the } the - voidPrintfromtoptobottom (binarytreenode*proot) in { the if(Proot = =NULL) the return; About thestd::d eque<binarytreenode *>Dequetreenode; the the Dequetreenode.push_back (proot); + - while(Dequetreenode.size ()) the {BayiBinarytreenode *pnode =Dequetreenode.front (); the Dequetreenode.pop_front (); the -printf"%c", pnode->m_nvalue); - the if(pnode->m_pleft) theDequetreenode.push_back (pnode->m_pleft); the the if(pnode->m_pright) -Dequetreenode.push_back (pnode->m_pright); the } the } the //Ordinary binary Tree94 //a the // / the //b C the // / / 98 //d E F About intMain () - {101 CharPreorder[treelen] = {'a','b','D','C','e','F'};102 CharInorder[treelen] = {'D','b','a','e','C','F'};103binarytreenode* result = Rebuild (preorder, inorder,6);104 Printfromtoptobottom (result); theprintf"\ n");106 Printtree (result);107}
Rebuilding a binary tree