剑指offer编程题——06 重建二叉树
2018-03-16 14:28
465 查看
#include<iostream> using namespace std; struct BinaryTreeNode{ int m_value; BinaryTreeNode* m_left; BinaryTreeNode* m_right; }; BinaryTreeNode* ConstructCore(int* startPre, int* endPre, int* startIn, int* endIn){ int rootv = startPre[0]; BinaryTreeNode* root = new BinaryTreeNode(); root->m_value = rootv; root->m_left = root->m_right = NULL; if (startPre == endPre){ //序列中只有一个值 if (startIn == endIn && *endPre == *endIn) return root; else throw exception("Invalid input!"); } int *rootIn = startIn; while (rootIn != endIn && *rootIn != rootv) ++rootIn; if (rootIn == endIn && *rootIn != rootv) //序列中有多个值 throw exception("Invalid input!"); int leftlen = rootIn - startIn; int *leftend = startPre + leftlen; if (leftlen > 0) root->m_left = ConstructCore(startPre + 1, leftend, startIn, rootIn - 1); if (leftlen < endPre - startPre) root->m_right = ConstructCore(leftend + 1, endPre, rootIn + 1, endIn); return root; } BinaryTreeNode* Construct(int* preorder, int* inorder, int len){ if (preorder == NULL || inorder == NULL || len <= 0) return NULL; return ConstructCore(preorder, preorder + len - 1, inorder, inorder + len - 1); } // ====================测试代码==================== void main06(){ //const int length = 8; //int pre[length] = { 1, 2, 4, 7, 3, 5, 6, 8 }; //int in[length] = { 4, 7, 2, 1, 5, 3, 8, 6 }; //const int length = 5; //int preorder[length] = { 1, 2, 3, 4, 5 }; //int inorder[length] = { 5, 4, 3, 2, 1 }; //const int length = 5; //int preorder[length] = { 1, 2, 3, 4, 5 }; //int inorder[length] = { 1, 2, 3, 4, 5 }; //const int length = 1; //int preorder[length] = { 1 }; //int inorder[length] = { 1 }; const int length = 7; int preorder[length] = { 1, 2, 4, 5, 3, 6, 7 }; int inorder[length] = { 4, 2, 8, 1, 6, 3, 7 }; BinaryTreeNode* root = Construct(preorder, inorder, length); if (root != nullptr){ printf("root value: %d\n", root->m_value); if (root->m_left != NULL) printf("root->left value: %d\n", root->m_left->m_value); if (root->m_right != NULL) printf("root->right value: %d", root->m_right->m_value); } }
相关文章推荐
- [剑指offer][面试题06]重建二叉树
- 剑指offer面试题06:重建二叉树
- 剑指offer 01-06解答思路以及代码(顺序数组找特定数字,替换空格字符,链表反转输出,重建二叉树,两个栈实现队列效果,旋转数组最小元素)
- 剑指Offer_面试题06_重建二叉树
- 【剑指offer-Java版】06重建二叉树
- 剑指offer-面试题06-重建二叉树
- 剑指offer 06 重建二叉树
- 【剑指offer】题目1385:重建二叉树
- 【剑指offer】面试题6:重建二叉树
- 剑指offer 编程题(58):序列化二叉树
- 剑指Offer学习总结-重建二叉树
- 剑指offer面试题6 重建二叉树(java)
- [剑指offer] 重建二叉树
- 剑指offer 面试题6 重建二叉树
- 剑指offer---重建二叉树
- [牛客网,剑指offer,python] 重建二叉树
- 剑指offer 面试题6—重建二叉树
- 剑指offer 重建二叉树 @python
- 剑指offer-重建二叉树
- 剑指offer---重建二叉树(6)