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. safari浏览器添加书签的方法

    说起来自己也是够笨的,竟然找了半天没发现safari的添加书签按钮.为了减少别的朋友遇到这个问题的解决时间. 我做下截图说明,其实就是safari的分享按钮. 希望对你能有所帮助

  2. get和post的区别与乱码问题解决

    ★ get和post的区别:     1.get请求通过url地址发送请求参数,可以在地址栏上直接显示     2.post请求通过请求体发送请求参数,不会再地址栏上显示     3.get在地址栏显 ...

  3. 史上最详细“截图”搭建Hexo博客——For Windows

    http://angelen.me/2015/01/23/2015-01-23-%E5%8F%B2%E4%B8%8A%E6%9C%80%E8%AF%A6%E7%BB%86%E2%80%9C%E6%88 ...

  4. Enabling Cross-Origin Requests in ASP.NET Web API 2

    Introduction This tutorial demonstrates CORS support in ASP.NET Web API. We’ll start by creating two ...

  5. NOSQL(一)--Redis

    简介 最近开始接触NoSQL,翻译过来就是 not only sql,非关系型数据库吧. 其中主要有四大类NoSQL,今天我们介绍其中的一种键值对的NoSQL:Redis. 定义:Redis是一个开源 ...

  6. php截取字符串函数

    public function sub_string($str, $len, $charset="utf-8") { if( !is_numeric($len) or $len & ...

  7. linux下memcached的安装

    系统镜像及环境要求: 1) 适用于windows系列版本及开发者的相关教程  请参考本文1.0开始安装步骤 2)  Centos 6系列及Aliyun Linux 6系列以上版本 请参考本文2.0开始 ...

  8. jqGrid 学习笔记--数据异步加载方法(转)

    var commonQuery = '../importantInfoReport/pageQueryImportantInfoReport.action?type=0'; jQuery(" ...

  9. JQuery中的extend函数

    1.jQuery.fn.extend(object) 扩展 jQuery 元素集来提供新的方法(通常用来制作插件). 例如:增加两个插件方法. jQuery.fn.extend({ check: fu ...

  10. 通过url 下载文件

    1.问题简介 通过文件的url,将文件下载到本地.文件存储的位置为:tomcat服务器的文件夹(通过读取properties文件:可看:http://www.cnblogs.com/0201zcr/p ...