[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 ...
随机推荐
- PHP实现QQ第三方登录的方法
前言: PHP实现QQ快速登录,罗列了三种方法 方法一:面向过程,回调地址和首次触发登录写到了一个方法页面[因为有了if做判断], 方法二,三:面向对象 1.先调用登录方法,向腾讯发送请求,2.腾讯携 ...
- Laravel数据库测试的另一种方案-SQLite
Laravel数据库测试 在测试方面,Laravel内置使用PHPUnit提供了非常方便的解决方案.而对于数据库增删改查的测试,要解决的一个很重要的问题就是如何在测试完成之后,恢复数据库的原貌,例如要 ...
- Why Countries Succeed and Fail Economically
Countries Succeed and Fail Economically(第一部分)" title="Why Countries Succeed and Fail Econo ...
- 基本dos
文件夹的操作: 进入指定盘符:盘符名+: dir:列出当前控制台下的所有文件以及文件夹 . cd +文件夹名称:进入指定文件夹 cd.. 返回上一级 cd \返回到当前目录的根目 ...
- CodeForcesGym 100524J Jingles of a String
Jingles of a String Time Limit: 2000ms Memory Limit: 524288KB This problem will be judged on CodeFor ...
- [K/3Cloud] 单据新增、复制、新增行、复制行的过程
整单复制:先执行CopyData(获得数据包),在执行AfterCreateNewData(可处理数据包),不会执行AfterCreateNewEntryRow 单据新增:先执行AfterCreate ...
- jsp页面根据json数据动态生成table
根据需求由于不同的表要在同一个jsp展示,点击某个表名便显示某张表内容,对于java后台传给jsp页面的json形式的数据是怎么动态生成table的呢? 找了好久,终于找到某位前辈的答案,在此表示衷心 ...
- EditText隐藏和显示
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- css3自定义流动条
<style> .item { height: 180px; overflow: auto; width: 180px; float: left; margin: 11px; box-sh ...
- 深入理解hadoop(二)
hadoop RPC 网络通信是hadoop的核心模块之一,他支撑了整个Hadoop的上层分布式应用(HBASE.HDFS.MapReduce), Hadoop RPC具有以下几个特性,透明性(用户本 ...