[LeetCode] Construct Binary Tree from Inorder and Pretorder 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.
Solution:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *build(vector<int> &preorder, vector<int> &inorder, int pre_start, int pre_end, int in_start, int in_end)
{
if(pre_start > pre_end || in_start > in_end)
return NULL;
TreeNode *curRoot = new TreeNode(preorder[pre_start]);
int rootIndex = -;
for(int i = in_start;i <= in_end;i++)
{
if(inorder[i] == preorder[pre_start])
{
rootIndex = i;
break;
}
}
if(rootIndex == -) return NULL;
int leftNum = rootIndex - in_start;
curRoot -> left = build(preorder, inorder, pre_start + , pre_start + leftNum, in_start, rootIndex - );
curRoot -> right = build(preorder, inorder, pre_start + leftNum + , pre_end, rootIndex + , in_end);
return curRoot;
} TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return build(preorder, inorder, , preorder.size() - , , inorder.size() - );
}
};
[LeetCode] Construct Binary Tree from Inorder and Pretorder Traversal的更多相关文章
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- Leetcode Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...
- LeetCode——Construct Binary Tree from Inorder and Postorder Traversal
Question Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may a ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- java IO流 内容整理
在java中,对数据的输入和输出操作以流的方式进行.(注:对文件的操作用io.File类,但不能对文件中的内容进行操作) 一.IO流的分类: 按数据流的方向不同,可以分为输入流和输出流: 按处理数据的 ...
- [Python3网络爬虫开发实战] 1.2.6-aiohttp的安装
之前介绍的Requests库是一个阻塞式HTTP请求库,当我们发出一个请求后,程序会一直等待服务器响应,直到得到响应后,程序才会进行下一步处理.其实,这个过程比较耗费资源.如果程序可以在这个等待过程中 ...
- Python和Java的语法对比,语法简洁上python的确完美胜出
Python是一种广泛使用的解释型.高级编程.通用型编程语言,由吉多·范罗苏姆创造,第一版发布于1991年.可以视之为一种改良(加入一些其他编程语言的优点,如面向对象)的LISP.Python的设计哲 ...
- python 爬虫示例,方便日后参考
参考网址:https://zhuanlan.zhihu.com/p/32037625 def getOneMoviesInfo(Mid,url): import requests from lxml ...
- 【BZOJ4710】分特产(容斥原理,组合计数)
题意:有m种特产,第i种有a[i]个 有n个同学分特产,要求: 1.恰好分完 2.每个人至少要分到一个 求方案数模10^9+7 n,m,a[i]<=1000 思路:WYZ作业 首先考虑对于每一种 ...
- 新vim配置文件
"******************************************************特殊设置************************************ ...
- Remove Duplicates from Sorted List (链表)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 线程池之ThreadPool与ForkJoinPool
网上对Java线程池都有很多非常具体的解析,我概念性进行总结下,如有错误,可与我联系修改. 1.1 ThreadPool Executor 一个线程池包括以下四个基本组成部分: 1.线程池管理器(Th ...
- Servlet的服务端响应
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/server-response.html: 当一个Web服务器对浏览器响应一个HTTP请求时,响应 ...
- 在Windows下搭建RocketMQ
原文:http://blog.csdn.net/u014134180/article/details/51790988 目录 目录 一 准备工作 1 RocketMQ部署架构1 2 环境配置 二 安装 ...