【leetocde】 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.
Subscribe to see which companies asked this question
#include<algorithm>
using namespace std;
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(preorder.size()==||inorder.size()==||find(inorder.begin(),inorder.end(),preorder[])==inorder.end())
return NULL;
auto rootindex=find(inorder.begin(),inorder.end(),preorder[]);
TreeNode *root = new TreeNode(preorder[]);
vector<int> subleft,subright;
preorder.erase(preorder.begin());
if(rootindex!=inorder.begin())
subleft.insert(subleft.begin(),inorder.begin(),rootindex);
if(rootindex!=inorder.end()-)
subright.insert(subright.begin(),rootindex+,inorder.end());
root->left=buildTree(preorder,subleft);
root->right=buildTree(preorder,subright);
return root;
}
};
【leetocde】 105. Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- 【原创】leetCodeOj ---Construct Binary Tree from Preorder and Inorder Traversal 解题报告
原题地址: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题目 ...
- [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 ...
- leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. ============== 基本功: 利用前序和 ...
- LeetCode OJ 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 ...
- 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 ...
随机推荐
- php接口加密
在实际开发中PHP接口开发是PHP常见的技术,可以通过接口进行一些操作,可以访问接口获取数据,给app提供数据接口等,但是如果接口没有加密解密的过程,很容易发生盗用和不安全的操作. 下边简单介绍一下A ...
- Oracle,Mysql ,SQL Server 三大数据库带参数的模糊查询, 拼接查询条件问题
最近项目开发一直在不断切换数据库,有时候一条sql 要同时考虑多种数据库中的兼容问题 , 先总结一条模糊查询拼接查询条件的问题,后续追加总结. 目前使用 mybatis: 1. Oracle 中使 ...
- [CF373C]计算袋鼠是愉快的(Counting Kangaroos is Fun)-贪心
Problem 计算袋鼠是愉快的 题目大意 有n只袋鼠,如果一个袋鼠体积是另一个袋鼠的两倍或以上,则小袋鼠能被大袋鼠装进袋子里,装进去后就看不到袋子里的袋鼠了,问这群袋鼠如何行动才能使得它们看着数量最 ...
- C#面试题记录
最怕的就是面试,每每到找工作的时候感觉压力山大,每次都要提前刷点题目去面对.所以这里自己对以后需要了解的知识做一个记录,方便自己随时的补充和深入了解,也给现在的自己留下一点点的印记,给未来留下一点回忆 ...
- Luogu 1111 修复公路(最小生成树)
Luogu 1111 修复公路(最小生成树) Description A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车.政府派人修复这些公路. 给出A地区的村庄数N,和公路数M,公路是双向的 ...
- Win10系统下安装Oracle服务器和Oracle客户端
工作电脑从Win7换为Win10,在给Win10系统安装Oracle时花费了很长世间终于搞定,在此给大家分享下. 1.工作中需要连接测试环境.生产环境Oracle,所以安装了公司封装的Oracle客户 ...
- [luogu P3384] 【模板】树链剖分 [树链剖分]
题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节点的值都加上z 操作2: 格式 ...
- 学会用requirejs,5分钟足矣
学会用requirejs,5分钟足矣 据说公司的项目较多的用到requirejs管理依赖,所以大熊同学挤出了5分钟休息时间学习了一下,现在分享一下.如果你想了解requirejs的实现原理,请绕道!如 ...
- JAVA入门[22]—thymeleaf
一.thymeleaf官网 官网:https://www.thymeleaf.org/index.html doc:https://www.thymeleaf.org/documentation.ht ...
- (转)rpm安装和卸载软件
场景:在Linux中经常需要安装一些rpm软件,但是有时候安装失误就需要卸载这些软件包. 1 过程记录 1.1 安装 rpm -i 需要安装的包文件名 举例如下: rpm -i example.rpm ...