【主题说明】givenpreorderandinordertraversalofatree,construct the binary tree。
Note:
youmayassumethatduplicatesdonotexistinthetree。
【解决问题的想法】与上面的问题相似
【查内容】树,递归
/* * definitionforbinarytree * struct treenode { * intval; * hcdbmhleft; * hcdbmhright; *treenode(intx ) : val(x ) left ) null )、right ) {} * }; */class solution { public : hcdbmhbuildtree (vectorintpreorder,vectorint inorder ) if ) preorder.size!=inorder.size () )返回空值; 返回构建(preorder,0,preorder.size(-1,inorder,0,inorder.size )-1 ); } treenode * build (vectorint preorder,int l1,int r1,vectorint inorder,int l2,int r2 ) if(L1R1||L2R2) return null int for(I=L2; inorder[i]!=r; I,cnt; hcdbmhroot=new treenode (preorder [ L1 ]; root-left=build(preorder,L1,l1 cnt,inorder,l2,i-1 ); root-right=build(preorder,l1 cnt 1,r1,inorder,i 1,r2 ); 返回根; }; `