剑指Offer--006-重构二叉树
来源:互联网 发布:mac能玩暗黑三吗 编辑:程序博客网 时间:2024/06/10 07:19
链接
牛客OJ:重建二叉树
九度OJ:http://ac.jobdu.com/problem.php?pid=1385
GitHub代码: 006-重建二叉树
CSDN题解:剑指Offer–006-重构二叉树
您也可以选择回到目录-剑指Offer–题集目录索引
题意
题目描述
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
输入
前序遍历序列{1,2,4,7,3,5,6,8}
中序遍历序列{4,7,2,1,5,3,8,6}
则重建二叉树并返回。
分析
这道题还是比较简单的,我们知道
* 前序遍历的顺序为:根左右
* 中序遍历的顺序为:左根右
递归思想
1. 我们先根据前序遍历序列的第一个确定根,然后在中序遍历的序列中找到根的位置,根左边的就是其左子树,右边就是其右子树
2. 构建根和左右子树
3. 递归的进行1和2
#include <iostream>#include <vector>using namespace std;// 调试开关#define __tmain main#ifdef __tmain#define debug cout#else#define debug 0 && cout#endif // __tmain#ifdef __tmainstruct TreeNode{ int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } // static void PreOrder(TreeNode *root) { if(root == NULL) { return; } cout <<root->val; PreOrder(root->left); PreOrder(root->right); } static void InOrder(TreeNode *root) { if(root == NULL) { return; } InOrder(root->left); cout <<root->val; InOrder(root->right); } };#endif // __tmain/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution{public: struct TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> in) { // 前序遍历的长度跟中序遍历的长度应该相同 if(pre.size( ) != in.size( )) { debug <<"the length of PRE and IN should be smae" <<endl; return NULL; } // 长度不能为0 int size = pre.size( ); if(size == 0) { debug <<"it's a NULL tree(length = 0)" <<endl; return NULL; } int length = pre.size( ); debug <<"the length of your tree = " <<length <<endl; int value = pre[0]; // 前序遍历的第一个结点是根节点 TreeNode *root = new TreeNode(value); debug <<"the root is" <<root->val <<endl; // 在中序遍历中查找到根的位置 int rootIndex = 0; for(rootIndex = 0; rootIndex < length; rootIndex++) { if(in[rootIndex] == value) { debug <<"find the root at " <<rootIndex <<" in IN" <<endl; break; } } if(rootIndex >= length) { debug <<"can't find root (value = " <<value <<") in IN" <<endl; return NULL; } /// 区分左子树和右子树 /// 中序遍历中, 根左边的就是左子数, 右边的就是右子树 /// 前序遍历中, 根后面是先遍历左子树, 然后是右子树 /// 首先确定左右子树的长度, 从中序遍历in中确定 int leftLength = rootIndex; int rightLength = length - 1 - rootIndex; debug <<"left length = " <<leftLength <<", rightLength = " <<rightLength <<endl; vector<int> preLeft(leftLength), inLeft(leftLength); vector<int> preRight(rightLength), inRight(rightLength); for(int i = 0; i < length; i++) { if(i < rootIndex) { // 前序遍历的第一个是根节点, 根后面的(leftLegnth = rootIndex) - 1个节点是左子树, 因此是i+1 preLeft[i] = pre[i + 1]; // 中序遍历前(leftLength = rootIndex) - 1个节点是左子树, 第rootIndex个节点是根 inLeft[i] = in[i]; debug <<preLeft[i] <<inLeft[i] <<" "; } else if(i > rootIndex) { // 前序遍历的第一个是根节点, 根后面的(leftLegnth = rootIndex) - 1个节点是左子树, 后面是右子树 preRight[i - rootIndex - 1] = pre[i]; // 中序遍历前(leftLength = rootIndex) - 1个节点是左子树, 第rootIndex个节点是根, 然后是右子树 inRight[i - rootIndex - 1] = in[i]; debug <<preRight[i - rootIndex - 1] <<inRight[i - rootIndex - 1] <<" "; } } debug <<endl <<"the left tree" <<endl; for(int i = 0; i < leftLength; i++) { debug <<preLeft[i] <<inLeft[i] <<" "; } debug <<endl; debug <<"the right tree" <<endl; for(int i = 0; i < rightLength; i++) { debug <<preRight[i] <<inRight[i] <<" "; } debug <<endl; root->left = reConstructBinaryTree(preLeft, inLeft); root->right = reConstructBinaryTree(preRight, inRight); return root; }};int __tmain( ){ int pre[] = { 1, 2, 4, 7, 3, 5, 6, 8 }; int in[] = { 4, 7, 2, 1, 5, 3, 8, 6 }; vector<int> preOrder(pre, pre + 8); vector<int> inOrder( in, in + 8); Solution solu; TreeNode *root = solu.reConstructBinaryTree(preOrder, inOrder); cout <<"PreOrder"; TreeNode::PreOrder(root); cout <<endl; cout <<"InOrder "; TreeNode::InOrder(root); cout <<endl; return 0;}
0 0
- 剑指Offer--006-重构二叉树
- 【剑指offer】重构二叉树
- 剑指offer--重构二叉树
- 剑指offer之重构二叉树
- 《剑指Offer》面试题:重构二叉树
- 剑指offer面试题6 重构二叉树
- 重构二叉树,java实现,剑指offer原题
- 剑指offer第四题(重构二叉树)
- 剑指Offer——面试题6:重构二叉树
- 剑指Offer: 二叉树
- 《剑指offer》006-重建二叉树
- 重构二叉树
- 重构二叉树
- 二叉树 重构
- 重构二叉树
- 重构二叉树
- 重构二叉树
- 重构二叉树
- Unity扩展 检视面板(Inspector面板),编辑器窗口(自己定义的类似Inspector面板的窗口),scene场景 常用的类说明
- 触发器记录对表记录操作
- MAC 如何设置文件夹权限为777
- Context 详解
- HDU 1076 An Easy Task
- 剑指Offer--006-重构二叉树
- 004.php变量类型
- 用java编程输出三角形状的星型图案
- linux查看系统启动时间
- 设计模式
- 惯性矩和偏心距描述器
- java socket 文件上传 有文件名
- 最近遇到的几个问题
- Hello World 之 AngularJS