Returns the result of a binary tree in ascending order and in descending order.
Suppose there is a tree that looks like this long. It's easy to know its pre-order traversal and mid-order traversal.
PreOrder: GDAFEMHZ
InOrder: ADEFGHMZ
PostOrder: AEFDHZMG
Now, if we only know the Pre-and mid-order traversal, how can we find the post-order traversal? For example, we know that the forward traversal of a tree is "GDAFEMHZ", while the central traversal is "ADEFGHMZ". How can we find the next traversal? <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + IDwvcD4KPHA + signature + IDwvcD4KPHA + tdq2/Signature + signature/Signature + dLU1/Signature + signature + IDwvcD4KPHA + signature + response + s3q1q6687LFu + response/response + response/bPMyse13bnptcSho8/response + Mjr1/PX08r31ti4tMnPw + response + zb/ j0tS7udSt0ru/w8r3wcuhozwvcD4KPHA + expires/expires + SzIt7ao1/expires + CjxwcmUgY2xhc3M9 "brush: java; "> // size is the number of nodes in the tree. BinTree * CreateBinTree (char * pre, char * inorder, int size) {if (size = 0) return NULL; binTree * root = new BinTree; root-> left = NULL; root-> right = NULL; root-> value = pre [0]; int I; for (I = 0; I Left = CreateBinTree (pre + 1, inorder, I); root-> right = CreateBinTree (pre + 1 + I, inorder + I + 1, size-i-1); return root ;} int main () {char pre [] = "ABDGCEFH"; char inorder [] = "DGBAECHF"; BinTree * root = CreateBinTree (pre, inorder, 8 ); postorder (root); return 0 ;}
In fact, the above Code is not concise enough. The question only requires output of subsequent traversal, and does not require achievements. Therefore, you do not need to calculate node-> left and node-> right, or return node. The improved version is as follows:
Struct TreeNode {struct TreeNode * left; struct TreeNode * right; char elem;}; void BinaryTreeFromOrderings (char * inorder, char * preorder, int length) {if (length = 0) {// cout <"invalid length"; return;} TreeNode * node = new TreeNode; // Noice that [new] shocould be written out. node-> elem = * preorder; int rootIndex = 0; for (; rootIndex <length; rootIndex ++) {if (inorder [rootIndex] = * preorder) break ;} // Left BinaryTreeFromOrderings (inorder, preorder + 1, rootIndex); // Right BinaryTreeFromOrderings (inorder + rootIndex + 1, preorder + rootIndex + 1, length-(rootIndex + 1 )); cout <
Elem <
Consider that the question only requires output of subsequent traversal, so it is not necessary to establish a node. You can directly store the value of the current node in a char and then output it.
#include
#include
#include
using namespace std;struct TreeNode{ struct TreeNode* left; struct TreeNode* right; char elem;};void BinaryTreeFromOrderings(char* inorder, char* preorder, int length){ if(length == 0) { //cout<<"invalid length"; return; } char node_value = *preorder; int rootIndex = 0; for(;rootIndex < length; rootIndex++) { if(inorder[rootIndex] == *preorder) break; } //Left BinaryTreeFromOrderings(inorder, preorder +1, rootIndex); //Right BinaryTreeFromOrderings(inorder + rootIndex + 1, preorder + rootIndex + 1, length - (rootIndex + 1)); cout<
<
<