编程之美3.9 重建二叉树
2013-09-10 22:00
393 查看
根据二叉树的前序和中序遍历序列构建二叉树。若有多个可能解,则输出一个。
struct NODE{ NODE* pLeft; NODE* pRight; char chValue; }; bool Rebuild(char* pPreOrder,char* pInOrder,int nTreeLen,NODE** pRoot) { if(nTreeLen==0) { *pRoot=NULL; return true; } *pRoot=new NODE(); (*pRoot)->chValue=pPreOrder[0]; int parentIndexIn=0; for(int i=0;i<nTreeLen;i++) { if (pPreOrder[0]==pInOrder[i]) { parentIndexIn=i; if(Rebuild(&pPreOrder[1],pInOrder,parentIndexIn,&((*pRoot)->pLeft)) &&Rebuild(&pPreOrder[parentIndexIn+1],&pInOrder[parentIndexIn+1],nTreeLen-parentIndexIn-1,&((*pRoot)->pRight))) return true; } } return false; } int main() { char* preOrder="abdccf"; char* inOrder="dbaccf"; NODE** pRoot=new NODE*; Rebuild(preOrder,inOrder,6,pRoot); return 0; }
相关文章推荐
- 《编程之美》3.9重建二叉树
- 《编程之美》3.9重建二叉树
- 编程之美系列: 3.9 重建二叉树
- 编程之美——3.9重建二叉树和3.10分层遍历二叉树(Java and C++)
- 重建二叉树(剑指offer6、编程之美3.9)
- 重建二叉树_《编程之美》3.9
- 重建二叉树(编程之美3.9)
- 《编程之美》读书笔记19: 3.9 重建二叉树
- 编程之美:第三章 结构之法 3.9重建二叉树
- 编程之美--重建二叉树
- 3.9重建二叉树
- 3.9 重建二叉树
- [编程之美] PSet3.9 重建二叉树
- 编程之美读书笔记_3.9_重建二叉树
- 编程之美-3.9-重建二叉树
- <编程之美>3.9重建二叉树
- 编程之美--3.9 重建二叉树
- 读书笔记之编程之美 - 3.9 重建二叉树
- 3.9重建二叉树(各种方案的分析比较及扩展问题的分析)
- 3.9重建二叉树(各种方案的分析比较及扩展问题的分析)