【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 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的更多相关文章
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- 【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 ...
- 【原创】leetCodeOj ---Construct Binary Tree from Preorder and Inorder Traversal 解题报告
原题地址: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题目 ...
- 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 ...
- 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- 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 ...
- (二叉树 递归) 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 ...
随机推荐
- linux基础命令学习(三)文件搜索 find
1.使用name选项 查找自己的根目录$Home中的文件,可以用: find ~ -name "*.log" -print 查找当前目录下的文件,可以用: find . -nam ...
- 转:IntelliJ IDEA 2016.1.3注册破解激活
IntelliJ IDEA 2016.1.3下载地址 https://download.jetbrains.8686c.com/idea/ideaIU-2016.1.3.exe 用注册码激活: 激活码 ...
- linux系统相关、硬件、资源 - 相关命令
分类命令: 1.1.系统 # uname -a # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue # 查看操作系统版本 # ...
- Ext.form.ComboBox常用属性详解
Ext.form.ComboBox常用属性详解 标签: Extjs js combo js 代码 var combo = new Ext.form.ComboBox({ store : new Ext ...
- 程序员应该知道的几个国外IT网站
程序员应该知道的几个国外IT网站 摘要:文中总结了几个常用的国外IT网站,下面列举出来供大家学习参考: 导读:文中总结了几个常用的国外IT网站,下面列举出来供大家学习参考: 1. TheServe ...
- 学习笔记:Tab Bar 控件使用详解
注意这里是:Tab Bar 不是Tab Bar Controller. Tab bar是继承UIView,所以可以添加到ViewController里.是View就可以add到另一个View上去.Ta ...
- Javascript:父类可以调用子类吗?
问:父类可以调用子类吗? 答:可以,经典的模板方法模式就是用的这个特性.
- 定制NSLog便于打印调试
定制NSLog便于打印调试 本人之前从事过嵌入式开发,对于打印调试比较在行,现分享定制的NSLog以及教大家如何使用. 源码下载地址 https://github.com/YouXianMing/Y ...
- 美国保健品品牌介绍之Now Foods
Now Foods是美国著名的美国保健品品牌,定位于大众品牌. 美国Now Foods公司位于美国伊利诺州,台湾中文名叫健而婷,成立于1968年,是美国保健品市场上名列三甲的国际知名的天然保健品牌,其 ...
- [Android Pro] git 打标签、推送tag到托管服务器、验证是否成功
reference to : http://www.cnblogs.com/ShaYeBlog/p/5576601.html 我们常常在代码封板时,使用git 创建一个tag ,这样一个不可修改的历史 ...