5.2.2—二叉树的构建—Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:mmd双人动作数据 编辑:程序博客网 时间:2024/06/08 15:32
描述
Given inorder and postorder traversal of a tree, construct the binary tree.
Note: You may assume that duplicates do not exist in the tree.
Given inorder and postorder traversal of a tree, construct the binary tree.
Note: You may assume that duplicates do not exist in the tree.
#include "BinaryTree.h"#include<vector>using namespace std;//===由后序和中序构建二叉树BinaryTreeNode* ConBinTreeFromPostAndMid(int post[],int postbegin, int mid[], int midbegin,int length){if (length <= 0) return NULL;BinaryTreeNode* proot = CreateBinaryTreeNode(post[postbegin+length-1]);ConnectTreeNodes(proot, NULL,NULL);int index = midbegin;for (int i = midbegin; i < midbegin+length; i++){if (post[postbegin + length - 1] == mid[i]){index = i; break;}}int prelength = index - midbegin;int lastlength = length - prelength-1;proot->m_pLeft=ConBinTreeFromPostAndMid(post, postbegin, mid, midbegin,prelength);proot->m_pRight = ConBinTreeFromPostAndMid(post, postbegin+prelength,mid ,index + 1,lastlength);return proot;}// ====================测试代码====================int main(){const int n = 9;int post[n] = { 7, 8, 5, 3, 4, 9, 10, 11,1};int mid[n] = { 7, 5, 8, 1, 3, 9, 4, 11,10};BinaryTreeNode *proot = ConBinTreeFromPostAndMid(post,0, mid,0,n);PrintTree(proot);DestroyTree(proot);}
阅读全文
0 0
- 5.2.2—二叉树的构建—Construct Binary Tree from Inorder and Postorder Traversal
- 5.2.1—二叉树的构建—Construct Binary Tree from Preorder and Inorder Traversal
- 5.2.2 Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode106—Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode(Construct Binary Tree from Inorder and Postorder Traversal)根据二叉树的中序和后续构造二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序遍历二叉树
- Construct Binary Tree from Inorder and Postorder Traversal(给出中序后序求二叉树)
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序重建二叉树
- Leetcode Construct Binary Tree from Inorder and Postorder Traversal 中序后序遍历重组二叉树
- 105\106. Construct Binary Tree from Inorder and Postorder\Preorder Traversal&重建二叉树
- leetcode:二叉树之Construct Binary Tree from Inorder and Postorder Traversal
- 重建二叉树106. Construct Binary Tree from Inorder and Postorder Traversal
- 【二叉树经典问题】106. Construct Binary Tree from Inorder and Postorder Traversal
- Tree——Construct Binary Tree from Inorder and Postorder Traversal
- *(leetcode) Construct Binary Tree from Inorder and Postorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder Traversal
- leetcode——106——Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode——Construct Binary Tree from Inorder and Postorder Traversal
- 数据类型,引用数据类型
- Codeforces837C two seals
- 快速查找素数 nyoj 187
- 在我眼中vue/vux的钩子
- IOS与其他操作系统中静态库与动态库的用法总结
- 5.2.2—二叉树的构建—Construct Binary Tree from Inorder and Postorder Traversal
- 5.3.3—二叉查找树—Validate Binary Sear Tree
- [VC.NET]托管代码和非托管代码
- 5.3.4—二叉查找树—Convert Sorted Array to Binary Sear Tree
- 5.3.5—二叉查找树—Convert Sorted List to Binary Sear Tree
- 动态规划中级教程 343. Integer Break
- 大型复杂信息系统研发的大学式组织管理方法
- UVA11754 Code Feat
- 坦克大战学习笔记