Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree.

分析: 根据前序遍历和中序遍历构造一棵树,递归求解即可

/**
* 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:
void Build(int l1,int l2, int r1,int r2, const vector<int>& pre, const vector<int> & in, TreeNode*& root){
root = new TreeNode(pre[l1]);
int i;
for(i=r1; i<=r2; i++)
if(in[i]==root->val)
break;
if(i==r1)
root->left=nullptr;
else
Build(l1+, l1+i-r1,r1, i- ,pre,in, root->left);
if(i==r2)
root->right=nullptr;
else
Build(l1+i-r1+, l2, i+, r2, pre, in, root->right);
return;
}
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(preorder.size()== && inorder.size()==)
return nullptr;
TreeNode* root;
Build(,preorder.size()-, , inorder.size()-, preorder, inorder, root);
return root;
}
};

Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

  1. 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...

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

  3. 【题解二连发】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 - LeetCode Construct Binary ...

  4. LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  5. 【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 ...

  6. [LeetCode] 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 ...

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

  8. 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal

    Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...

  9. Leetcode 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 ...

随机推荐

  1. iOS 学习 - 11.圆角(小于等于四个)类似气泡和计算字符高度

    使用贝塞尔曲线, // 小于四个角 圆角 -(void)setbor{ NSString *str = @" couldn't fit this all in a comment to @l ...

  2. ThinkPHP的RBAC原理分析

    转载 http://jingyan.baidu.com/article/a948d65151a4810a2ccd2e60.html 方法/步骤 多用户对节点的操作的权限控制,这句话,经过反复扩展功能, ...

  3. 集群服务器 时间同步 - Chrony

    greenplum,openstack等云计算项目需要集群服务器部署,服务器之间的时间需要同步,但并不是所有机器可以 直接连外网,这时可以用Chrony工具解决. 解决方法是将其中一台设为时间服务器, ...

  4. (视频) 《快速创建网站》 2.3 WordPress初始化和功能简介

    本文是<快速创建网站>系列的第4篇,如果你还没有看过之前的内容,建议你点击以下目录中的章节先阅读其他内容再回到本文. 访问本系列目录,请点击:http://devopshub.cn/tag ...

  5. ARC下的所有权修饰符

    ARC有效时,id类型必须加上所有权修饰符 下面为三种等效的声明,为了便于和二级指针的情况联系起来,采用第一种. NSError * __weak error = nil; NSError __wea ...

  6. jquery.validate remote的用法

    1,远程返回数据时,一定要返回"true"或者"false",否则就是永远就是验证不通过. 2,remote有两种方式,如下就介绍remote与PHP间的验证( ...

  7. java 中的volatile

    本博客摘录自   http://www.infoq.com/cn/articles/java-memory-model-4/ 当我们声明共享变量为volatile后,对这个变量的读/写将会很特别.理解 ...

  8. 含有SilverLight项目的代码重用

    很多时候,我们工程中不只是有SilverLight项目,然而我们想把其他的项目中的工具类,或者实体类复用到SilverLight项目中时,会发现SilverLight项目无法使用.这是个很普遍的现象. ...

  9. 4、解决native库不兼容

    解决native库不兼容 现象: 报警告 [root@hadoop1 hadoop-]# bin/hdfs dfs -ls /input // :: WARN util.NativeCodeLoade ...

  10. SSH框架配置释义

      创建一个bean, 依赖注入支持的不同注入方式及具体配置方法 属性注入 通过setter()方法注入bean的属性或者依赖对象.属性注入要求bean需要提供一个默认构造函数和对应的setter方法 ...