5.2.1—二叉树的构建—Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:开淘宝网店详细步骤 编辑:程序博客网 时间:2024/06/03 23:38
描述
Given preorder and inorder traversal of a tree, construct the binary tree.
Note: You may assume that duplicates do not exist in the tree.
Given preorder and inorder 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* ConBinTreeFromPreAndMid(int pre[],int prebegin, int mid[], int midbegin,int length){if (length <= 0) return NULL;BinaryTreeNode* proot = CreateBinaryTreeNode(pre[prebegin]);ConnectTreeNodes(proot, NULL,NULL);int index = midbegin;for (int i = midbegin; i < midbegin+length; i++){if (pre[prebegin] == mid[i]){index = i; break;}}int prelength = index - midbegin;int lastlength = length - prelength-1;proot->m_pLeft=ConBinTreeFromPreAndMid(pre, prebegin+1, mid, midbegin,prelength);proot->m_pRight = ConBinTreeFromPreAndMid(pre, prebegin+prelength+1,mid ,index + 1,lastlength);return proot;}// ====================测试代码====================int main(){const int n = 9;int pre[n] = { 1, 5, 7, 8, 11, 9, 3, 4,10};int mid[n] = { 7, 5, 8, 1, 3, 9, 4, 11,10};BinaryTreeNode *proot = ConBinTreeFromPreAndMid(pre,0, mid,0,n);PrintTree(proot);DestroyTree(proot);}
阅读全文
0 0
- 5.2.1—二叉树的构建—Construct Binary Tree from Preorder and Inorder Traversal
- 5.2.2—二叉树的构建—Construct Binary Tree from Inorder and Postorder Traversal
- 5.2.1 Construct Binary Tree from Preorder and Inorder Traversal
- 5.2.1 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode105—Construct Binary Tree from Preorder and Inorder Traversal
- leetcode---Construct Binary Tree from Preorder and Inorder Traversal---树的构建
- Construct Binary Tree from Preorder and Inorder Traversal 前序和中序遍历构建二叉树
- 105. Construct Binary Tree from Preorder and Inorder Traversal 递归构建树
- 105. Construct Binary Tree from Preorder and Inorder Traversal,前序+中序 构建 树
- Construct Binary Tree from Preorder and Inorder Traversal (给出前序中序求二叉树)
- Construct Binary Tree from Preorder and Inorder Traversal 前序和中序重建二叉树
- Leetcode Construct Binary Tree from Preorder and Inorder Traversal 前序中序遍历重组二叉树
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal(构造二叉树)
- 105\106. Construct Binary Tree from Inorder and Postorder\Preorder Traversal&重建二叉树
- leetcode:二叉树之Construct Binary Tree from Preorder and Inorder Traversal
- 重建二叉树105. Construct Binary Tree from Preorder and Inorder Traversal
- 【二叉树经典问题】105. Construct Binary Tree from Preorder and Inorder Traversal
- *(leetcode) Construct Binary Tree from Preorder and Inorder Traversal (tree)
- hdu 6070 Dirt Ratio(二分+线段树)(2017 Multi-University Training Contest
- 5.1.12—二叉树的遍历—Populating Next Right Pointers in Ea Node II
- 开发环境配置(hibernate4)和简单增删改查
- php使用phpexcel类操作excel文件数据
- docker~从Dockerfile到Container的过程(终于算是OK了)
- 5.2.1—二叉树的构建—Construct Binary Tree from Preorder and Inorder 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]托管代码和非托管代码