I wrote the following code to build a tree from my order traversal orders and preorders. This looks right to me, but the last tree that results from it does not have the same inorder output as the one from which it was built. Can someone help me find a flaw in this feature?
public btree makeTree(int[] preorder, int[] inorder, int left,int right) { if(left > right) return null; if(preIndex >= preorder.length) return null; btree tree = new btree(preorder[preIndex]); preIndex++; int i=0; for(i=left; i<= right;i++) { if(inorder[i]==tree.value) break; } tree.left = makeTree(preorder, inorder,left, i-1); tree.right = makeTree(preorder, inorder,i+1, right ); return tree; }
Note: preIndex is a static declaration outside the function.
in = {1,3,2,5}; pre = {2,1,5,3};
"". pre , 2 , in , {1,3} , {5} :
pre
2
in
{5}
2 / \ / \ {1,3} {5}
, 3 pre, . : {2,1,3,5} {2,3,1,5}. {2,1,5,3} .
3
{2,1,3,5}
{2,3,1,5}
{2,1,5,3}
, , . , , in[] pre[]?
in[]
pre[]
Source: https://habr.com/ru/post/1754756/More articles:.NET 3.0 или более поздняя версия Socket Bible - c#How to resize my vb6 program so that it automatically matches any screen resolution? - vb6How to change background / foreground color in ReadOnly textbox? - windows-phone-7NUnit: how to check private method with parameter "ref" in C # - c #Drawable and NetBeans Resources (Android Development) - androidVideo Capture - androidPHP Checksum before include () - securityhow to make drupal submit form using ajax? - jqueryFlex AdvancedDataGrid sort - sortingfinding an easy way to change multiple tables at once in mysql - mysqlAll Articles