题目

Given preorder and inorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

思路

主要是根据前序遍历和中序遍历的特点解决这个题目。

1、确定树的根节点。树根是当前树中所有元素在前序遍历中最先出现的元素。
2、求解树的子树。找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元素就是右子树。若根节点左边或右边为空,则该方向子树为空;若根节点边和右边都为空,则根节点已经为叶子节点。
3、递归求解树。将左子树和右子树分别看成一棵二叉树,重复1、2、3步,直到所有的节点完成定位

代码

/*---------------------------------------
*   日期:2015-04-28
*   作者:SJF0115
*   题目: 105.Construct Binary Tree from Preorder and Inorder Traversal
*   网址:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;struct TreeNode{int val;TreeNode *left;TreeNode *right;TreeNode(int x):val(x),left(nullptr),right(nullptr){}
};class Solution {
public:TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {int size = preorder.size();if(size <= 0){return nullptr;}//ifreturn PreInBuildTree(preorder,inorder,0,0,size);}
private:TreeNode* PreInBuildTree(vector<int> &preorder,vector<int> &inorder,int preIndex,int inIndex,int size){if(size <= 0){return nullptr;}//if// 根节点TreeNode* root = new TreeNode(preorder[preIndex]);// 寻找根节点在中序遍历数组的下标int index = 0;for(int i = 0;i < size;++i){if(preorder[preIndex] == inorder[inIndex+i]){index = inIndex+i;break;}//if}//for// 左子树个数int leftSize = index - inIndex;// 右子树个数int rightSize = size - leftSize - 1;// 左子树root->left = PreInBuildTree(preorder,inorder,preIndex+1,inIndex,leftSize);// 右子树root->right = PreInBuildTree(preorder,inorder,preIndex+1+leftSize,index+1,rightSize);return root;}
};void PostOrder(TreeNode* root){if(root){PostOrder(root->left);PostOrder(root->right);cout<<root->val<<endl;}//if
}int main(){Solution solution;vector<int> preorder = {1,2,4,8,5,3,6,7};vector<int> inorder = {8,4,2,5,1,6,3,7};TreeNode* root = solution.buildTree(preorder,inorder);// 输出PostOrder(root);return 0;
}

运行时间

[LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal相关推荐

  1. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...

  2. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 本博客转载自:http://www.cnblogs.co ...

  3. LeetCode: 105. Construct Binary Tree from Preorder and Inorder Traversal

    题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...

  4. LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal-前序中序遍历构造二叉树-Python和Java递归解法

    题目地址:Construct Binary Tree from Preorder and Inorder Traversal - LeetCode Given preorder and inorder ...

  5. leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)...

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...

  6. LeetCode OJ - Construct Binary Tree from Preorder and Inorder Traversal

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...

  7. LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...

  8. [leetcode] Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  9. [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树...

    Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...

最新文章

  1. web前端入门学习 html5(1)
  2. 远程Linux主机安装zsh插件zsh-syntax-highlighting
  3. 基于知识图谱的直升机飞行指挥模型研究
  4. 开红数显示服务器为空,网维大师常见问题:图标空白或红号问号
  5. java sbyte_Java Byte parseByte()方法
  6. 沈南鹏:移动互联网会带来9大创新性的领域
  7. Day 41 Rsync备份
  8. 如何使用融云地图,文件等插件--融云 Android SDK 2.8.0+ Extension 开发文档
  9. 人脸识别十大关键技术
  10. Typora Beta版过期解决方法
  11. 阿里云 ADAM 迁移工具测试问题记录
  12. CUDA的Occupancy和Achieved Occupancy概念
  13. math库是python语言的数学模块对不对_12.下列属于math库中的数学函数的是( )。
  14. minecraft1.16java_我的世界:1.16最神奇的种子,自然生成的石头雕像,基岩能用!...
  15. win10右键新建没有文本文档
  16. QQ聊天记录快速迁移
  17. 江西计算机奥赛试题初赛小学组,STEAM教育丨江西省第十八届中小学电脑制作技能提升活动创客竞赛圆满落幕!...
  18. python word操作添加超链接_使用pythondocx在MSWord中添加超链接
  19. js原生,省市区三级联动插件
  20. mysql explain介绍

热门文章

  1. 轻轻的我走了,正如我轻轻的来…——ADO.NET核心类的灭度与SQLHelper的诞生——十八相送(下)...
  2. 多核时代 .NET Framework 4 中的并行编程6---并行LINQ
  3. 自画菜单中如何触发MeasureItem事件的问题及解决办法
  4. CVPR论文 | 所见所想所找:基于生成模型的跨模态检索
  5. 2011 ScrumGathering敏捷个人.pptx
  6. js数组去重(多种方法)
  7. 2017qcon大会的一点想法(安全人才如何不被淘汰?)
  8. 关于PKI架构(使用证书)保护Web访问的安全实现SSL的基本理论
  9. 陶哲轩实分析 习题 7.2.6 (嵌套级数)
  10. CreateWindow创建指定宽和高的client区域窗口的方法