按照前序遍历和中序遍历构建二叉树
来源:互联网 发布:ps制作淘宝模板 编辑:程序博客网 时间:2024/06/05 18:48
转载自:http://blog.csdn.net/sbitswc/article/details/26433051
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
There is an example.
_______7______ / \ __10__ ___2 / \ / 4 3 _8 \ / 1 11
The preorder and inorder traversals for the binary tree above is:
preorder = {7,10,4,3,1,2,8,11}inorder = {4,10,3,1,7,11,8,2}
The first node in preorder alwasy the root of the tree. We can break the tree like:
1st round:
preorder: {7}, {10,4,3,1}, {2,8,11}
inorder: {4,10,3,1}, {7}, {11, 8,2}
_______7______ / \ {4,10,3,1} {11,8,2}
Since we alreay find that {7} will be the root, and in "inorder" sert, all the data in the left of {7} will construct the left sub-tree. And the right part will construct a right sub-tree. We can the left and right part agin based on the preorder.
2nd roundleft part right part
preorder: {10}, {4}, {3,1} {2}, {8,11}
inorder: {4}, {10}, {3,1} {11,8}, {2}
_______7______ / \ __10__ ___2 / \ / 4 {3,1} {11,8}
see that, {10} will be the root of left-sub-tree and {2} will be the root of right-sub-tree.
Same way to split {3,1} and {11,8}, yo will get the complete tree now.
_______7______ / \ __10__ ___2 / \ / 4 3 _8 \ / 1 11
So, simulate this process from bottom to top with recursion as following code.
c++- TreeNode *BuildTreePI(
- vector<int> &preorder,
- vector<int> &inorder,
- int p_s, int p_e,
- int i_s, int i_e){
- if(p_s > p_e) return NULL;
- int pivot = preorder[p_s];
- int i = i_s;
- for(;i<i_e;i++){
- if(inorder[i] == pivot)
- break;
- }
- int length1 = i-i_s-1;
- int length2 = i_e-i-1;
- TreeNode* node = new TreeNode(pivot);
- node->left = BuildTreePI(preorder,inorder,p_s+1,length1+p_s+1,i_s, i-1);
- node->right = BuildTreePI(preorder, inorder, p_e-length2, p_e, i+1, i_e);
- return node;
- }
- TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
- return BuildTreePI(preorder,inorder,0,preorder.size()-1,0,inorder.size()-1);
- }
java
- public TreeNode buildTree(int[] preorder, int[] inorder) {
- return buildPI(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1);
- }
- public TreeNode buildPI(int[] preorder, int[] inorder, int p_s, int p_e, int i_s, int i_e){
- if(p_s>p_e)
- return null;
- int pivot = preorder[p_s];
- int i = i_s;
- for(;i<i_e;i++){
- if(inorder[i]==pivot)
- break;
- }
- TreeNode node = new TreeNode(pivot);
- int lenLeft = i-i_s;
- node.left = buildPI(preorder, inorder, p_s+1, p_s+lenLeft, i_s, i-1);
- node.right = buildPI(preorder, inorder, p_s+lenLeft+1, p_e, i+1, i_e);
- return node;
- }
0 0
- 按照前序遍历和中序遍历构建二叉树
- 根据中序和前序遍历构建二叉树
- 根据前序遍历,中序遍历构建二叉树
- c++ 二叉树的构建 前序遍历 中序遍历 后续遍历 层次遍历
- 第四十三题 通过前序遍历和中序遍历构建二叉树
- 根据前序遍历和中序遍历构建二叉树
- 根据中序遍历和前序遍历构建二叉树
- Java实现-前序遍历和中序遍历构建二叉树
- 由前序遍历和中序遍历重新构建二叉树
- 根据前序遍历和中序遍历构建二叉树以及根据中序遍历后序遍历构建二叉树
- java实现根据前序遍历构建二叉树(前序遍历、中序遍历、后序遍历)
- 根据前序遍历、中序遍历构建二叉树,并后序遍历输出。
- 中序遍历+后序遍历/前序遍历构建二叉树
- 前序遍历和中序遍历重建二叉树
- 前序遍历和中序遍历重建二叉树
- 已知二叉树的前序和中序遍历,构建该二叉树
- 已知二叉树的前序和中序遍历,构建该二叉树
- 中序遍历和先序遍历构建二叉树
- GDAL(OGR)库
- Android Studio:xxx is not an enclosing class 错误的解决方法
- View requires API level 14 (current min is 8)
- Redis 五种数据类型的使用场景
- ListView多图片下载以及RecyclerView代替ListView与GridView
- 按照前序遍历和中序遍历构建二叉树
- Android xUtils框架
- Java文件操作
- Android的Context && 安卓常用系统服务(当前运行包名/当前网络状态和开关网络/音频服务/马达服务)
- SAP 关于ABAP TABLE BUFFER的解释
- Jquery基本知识点的总结
- Android 使用NineOldAndroids实现绚丽的ListView左右滑动删除Item效果
- 头插法创建单链表
- Google浏览器Chrome安装失败解决办法