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. PS和AI安装后报代码为16的错误解决方法

    1.问题 2.解决方式 右击属性,改为兼容性运行 参考文章地址:https://www.jb51.net/softjc/308950.html

  2. mysql出现问题汇总(持续更新)

    1.mysql -uqwe -p普通用户登陆时提示: ERROR 1045 (28000): Access denied for user 'baijie'@'%' (using password: ...

  3. [Python3网络爬虫开发实战] 3.1.4-分析Robots协议

    利用urllib的robotparser模块,我们可以实现网站Robots协议的分析.本节中,我们来简单了解一下该模块的用法. 1. Robots协议 Robots协议也称作爬虫协议.机器人协议,它的 ...

  4. LAMP中添加多虚拟主机

    在/etc/apache2/sites-available中默认有个default文件,其中的大致配置如下: <VirtualHost *:80> ServerAdmin xujie198 ...

  5. Yum:更换aliyun的yum源

    备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup centos7 wget -O /et ...

  6. 4. GC 算法(实现篇) - GC参考手册

    您应该已经阅读了前面的章节: 垃圾收集简介 - GC参考手册 Java中的垃圾收集 - GC参考手册 GC 算法(基础篇) - GC参考手册 学习了GC算法的相关概念之后, 我们将介绍在JVM中这些算 ...

  7. UVaLive 4868 Palindrometer (暴力 / 构造)

    题意: 给定一个固定长度的字符串, 字符串是一个含有前导0的数字, 问这个数字加上多少能构成一个回文字符串. 分析: 其实这题有很多种方法, 方法12是我做完后看别人代码总结的, 方法3是我当时想的一 ...

  8. 最详细的JavaWeb开发基础之java环境搭建(Mac版)

    阅读文本大概需要 5 分钟. 我之前分享过在 Windows 下面配置 Java 环境,这次给大家带来的是 Mac 下面安装配置 Java 环境.首先 Mac 系统已经带有默认的 Java,但是由于使 ...

  9. 关于PHP include文件时的文件查找顺序

    常常被include文件的路径搞晕. 看来是要理一理的时候了. PHP官方文档关于include搜索路径的解释是:先查找工作目录下相对于include_path设置所对应的路径,然后再搜索执行文件所在 ...

  10. 【CodeChef】KNGHTMOV(方案数DP)

    题意: 考虑一张无限大的方格棋盘.我们有一个“骑士”,它必须从(0,0)格开始,按照如下规则,移动至(X,Y)格:每一步,它只能从(u,v)格移动至(u+Ax,v+Ay)或者(u+Bx,v+By).注 ...