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 duplicates do not exist in the tree.

可与Construct Binary Tree from Inorder and Postorder Traversal对照来看。

前序遍历Preorder的第一个节点为根,由于没有重复值,可使用根将中序Inorder划分为左右子树。

递归下去即可。

/**
* 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 *buildTree(vector<int> &preorder, vector<int> &inorder) {
return Helper(preorder, , preorder.size()-, inorder, , inorder.size()-);
} TreeNode* Helper(vector<int> &preorder, int begin1, int end1, vector<int> &inorder, int begin2, int end2)
{
if(begin1 > end1)
return NULL;
else if(begin1 == end1)
return new TreeNode(preorder[begin1]); TreeNode* root = new TreeNode(preorder[begin1]);
int i = begin2;
for(; i <= end2; i ++)
{
if(inorder[i] == preorder[begin1])
break;
}
//inorder[i] is the root
int leftlen = i-begin2; //preorder[begin1] is root
//inorder[begin2+leftlen] is root
root->left = Helper(preorder, begin1+, begin1+leftlen, inorder, begin2, begin2+leftlen-);
root->right = Helper(preorder, begin1+leftlen+, end1, inorder, begin2+leftlen+, end2);
return root;
}
};

【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

  1. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  2. 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  3. 【leetocde】 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 ...

  4. 【原创】leetCodeOj ---Construct Binary Tree from Preorder and Inorder Traversal 解题报告

    原题地址: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题目 ...

  5. LeetCode OJ 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 ...

  6. 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

  9. (二叉树 递归) 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 ...

随机推荐

  1. JQ 数组动态添值,对象动态添值,判断数组/对象是否为空

    1.数组动态添值 首先声明一个空数组:var data = new Array(); 向数组中添值 :data.push('添加的值'); 示例:

  2. 关于npm run build 卡住不动的坑。。。

    起因:最近开发个项目使用的vue,有些功能需要生产版本放服务器上测试,这就出问题了..卡住了,不动了,还不报错 这是在vscode的终端里面,试了git hash,试了cmd , 试了powershe ...

  3. Turn any Linux computer into SOCKS5 proxy in one command

    src: http://www.catonmat.net/blog/linux-socks5-proxy/ I thought I'd do a shorter article on catonmat ...

  4. 使用openURL实现程序间带参数跳转详解

    使用openURL实现程序间带参数跳转详解 实现的效果:有两款应用A与B,A打开B A --> B 1. 新建工程B,bundle ID为com.YouXianMing.B 建立一个URL 这么 ...

  5. 开始学习深度学习和循环神经网络Some starting points for deep learning and RNNs

    Bengio, LeCun, Jordan, Hinton, Schmidhuber, Ng, de Freitas and OpenAI have done reddit AMA's.  These ...

  6. iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)

    两种创建表格方式的比较:表格视图.集合视图(二者十分类似) <1>相同点:   表格视图:UITableView(位于storyboard中,通过UIViewController控制器实现 ...

  7. 数学图形(2.14)Spherical helix曲线

    从http://mathworld.wolfram.com/SphericalHelix.html上找到如下一些关于该曲线的说明,不过似乎他的公式和我的脚本完全是两个东西.. The tangent  ...

  8. SpringMVC的学习

    在看<跟开涛学SpringMVC.pdf> /Users/baidu/Documents/Data/Interview/Java Spring Web MVC 也是服务到工作者模式的实现, ...

  9. tornado框架介绍

    一.安装tornado 手动安装: 下载 tornado-1.2.1.tar.gz tar xvzf tornado-1.2.1.tar.gz cd tornado-1.2.1 python setu ...

  10. DRP——JDBC中的Batch

    在jdbc2.0里添加了批量处理的功能(batch),其同意将多个sql语句作为一个单元送至数据库去运行,这样做能够提高操作效率.在操作大量的数据时, ORM框架实现批量是非常慢的.我们能够使用jdb ...