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的更多相关文章

  1. 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 ...

  2. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  3. [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 ...

  4. 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 ...

  5. [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...

  6. 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 ...

  7. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  8. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  9. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

随机推荐

  1. tcpdump用于抓取tcp数据包

    一.简单使用:-c监听次数.-v打印详情.host后接监听地址 1.1.监听 tcpdump -c -v host www.baidu.com 1.2.访问被监听的网址: 1.3.查看监听的数据:

  2. Linux最常用的基础命令 上篇

    Linux最常用的基础命令个人总结 计算机基础知识 32bit和64bit系统的区别.系统运行机制 1989年python 诞生 C语言是编译型的语言,不太支持跨平台 Django 江购 32bit= ...

  3. JqueryValidate 修改 为根据ID验证

    <!--修改validate根据ID验证 --> <script type="text/javascript"> if ($.validator) { $. ...

  4. jquery点击事件

    $("#hoetelNameSelect").click( $.post("${ctx}/meeting/restaurant/queryHotelName", ...

  5. PowerShell Tools for Visual Studio 2015

    首先要去下载Visual Studio 2015 RC 版本 https://www.visualstudio.com/en-us/downloads/visual-studio-2015-downl ...

  6. Dijkstra算法C++实现总结

    问题描述 求无负权图中点s到点t的最短凝聚力 备注 标准说法中,"缩短"/"松弛"(relax)操作是对边进行的.下面为了行文方便,将其拓展到点.即以下操作,其 ...

  7. I - Balancing Act POJ - 1655

    Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the t ...

  8. Count the Colors-ZOJ1610(线段树区间求)

    Painting some colored segments on a line, some previously painted segments may be covered by some th ...

  9. Ubuntu 16.04 LTS GNOME版本下载

    下载地址: http://cdimage.ubuntu.com/ubuntu-gnome/releases/ Ubuntu GNOME发行版本启动已经有三年的时间了,在社区用户对于在稳定可靠的Ubun ...

  10. Ionic3 填坑记录 - java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

    1  错误:Unable to merge dex 执行打包命令时 ionic cordova build android --prod 报如下错误 2 原因 重复引用了同一个包 如上图所示, com ...