[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 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相关推荐
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 本博客转载自:http://www.cnblogs.co ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
- [leetcode] Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- [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 ...
最新文章
- web前端入门学习 html5(1)
- 远程Linux主机安装zsh插件zsh-syntax-highlighting
- 基于知识图谱的直升机飞行指挥模型研究
- 开红数显示服务器为空,网维大师常见问题:图标空白或红号问号
- java sbyte_Java Byte parseByte()方法
- 沈南鹏:移动互联网会带来9大创新性的领域
- Day 41 Rsync备份
- 如何使用融云地图,文件等插件--融云 Android SDK 2.8.0+ Extension 开发文档
- 人脸识别十大关键技术
- Typora Beta版过期解决方法
- 阿里云 ADAM 迁移工具测试问题记录
- CUDA的Occupancy和Achieved Occupancy概念
- math库是python语言的数学模块对不对_12.下列属于math库中的数学函数的是( )。
- minecraft1.16java_我的世界:1.16最神奇的种子,自然生成的石头雕像,基岩能用!...
- win10右键新建没有文本文档
- QQ聊天记录快速迁移
- 江西计算机奥赛试题初赛小学组,STEAM教育丨江西省第十八届中小学电脑制作技能提升活动创客竞赛圆满落幕!...
- python word操作添加超链接_使用pythondocx在MSWord中添加超链接
- js原生,省市区三级联动插件
- mysql explain介绍
热门文章
- 轻轻的我走了,正如我轻轻的来…——ADO.NET核心类的灭度与SQLHelper的诞生——十八相送(下)...
- 多核时代 .NET Framework 4 中的并行编程6---并行LINQ
- 自画菜单中如何触发MeasureItem事件的问题及解决办法
- CVPR论文 | 所见所想所找:基于生成模型的跨模态检索
- 2011 ScrumGathering敏捷个人.pptx
- js数组去重(多种方法)
- 2017qcon大会的一点想法(安全人才如何不被淘汰?)
- 关于PKI架构(使用证书)保护Web访问的安全实现SSL的基本理论
- 陶哲轩实分析 习题 7.2.6 (嵌套级数)
- CreateWindow创建指定宽和高的client区域窗口的方法