Leetcode 之Construct Binary Tree(52)
根据先序和中序构造二叉树、根据中序和后序构造二叉树,基础题,采用递归的方式解决,两题的方法类似。需要注意的是迭代器的用法。
//先序和中序
TreeNode *buildTree(vector<int>& preorder, vector<int>& inorder)
{
return buildTree(begin(preorder), end(preorder), begin(inorder), begin(inorder));
}
template<typename InputIterator>
TreeNode *buildTree(InputIterator pre_first, InputIterator pre_last,
InputIterator in_first, InputIterator in_last)
{
if (pre_first == pre_last)return nullptr;
if (in_first == in_last)return nullptr;
//先序第一个为根结点
auto root = new TreeNode(*pre_first);
//查找根结点在中序的位置,返回的是迭代器
auto intRootPos = find(in_first, in_last, *pre_first);
//得到根结点的左半部分
auto leftSize = distance(in_first, intRootPos);
//递归构造,注意去掉根结点
root->left = buildTree(next(pre_first), next(pre_first, leftSize),
in_first, next(in_first, leftSize));
root->right = buildTree(next(pre_first, leftSize), pre_last, next(intRootPos), in_last); return root;
}
//中序和后序
TreeNode *buildTree1(vector<int>& inorder, vector<int>& postorder)
{
return buildTree1(begin(inorder), end(inorder), begin(postorder), begin(postorder));
}
template<typename InputIterator>
TreeNode *buildTree1(InputIterator in_first, InputIterator in_last,
InputIterator post_first, InputIterator post_last)
{
if (in_first == in_last)return nullptr;
if (post_first == post_last)return nullptr;
//后序最后一个为根结点
const auto val = *prev(post_last)
auto root = new TreeNode(val);
//查找根结点在中序的位置,返回的是迭代器
auto intRootPos = find(in_first, in_last, val);
//得到根结点的左半部分
auto leftSize = distance(in_first, intRootPos);
//递归构造,注意去掉根结点
root->left = buildTree(in_first, intRootPos,
post_first, next(post_first, leftSize));
root->right = buildTree(next(intRootPos), in_last,
next(post_first,leftSize), prev(post_last)); return root;
}
Leetcode 之Construct Binary Tree(52)的更多相关文章
- (二叉树 递归) 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 106. 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 ...
- [LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [LeetCode] 106. 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 ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
- [LeetCode] 889. Construct Binary Tree from Preorder and Postorder Traversal 由先序和后序遍历建立二叉树
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- [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 ...
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
随机推荐
- 制衡技术,从Adblock所想到的
这是一个很特别的东西.可能已经有人发现了它,但是它并非被广泛接受. 对于这个技术的思考来源主要是在安装了Adblock后想到的.这种反作用力的技术,很可能是一片蓝海.而这种技术的产生及推广,对未来社会 ...
- MongoDB驱动之Linq操作
添加下面命名空间到您的程序中: using MongoDB.Driver.Linq; 声明一变量保存对集合的引用 var collection = database.GetCollection< ...
- php中命名空间的使用
简单使用 命名空间主要解决函数/类冲突的问题.由于PHP中中不允许函数重载,所以我们要使用的到命名空间的.先看一个简单的例子. <?php namespace A; public functio ...
- AngularJS - 服务简介
服务是AngularJS中非常重要的一个概念,虽然我们有了控制器,但考虑到其生命实在脆弱,我们需要用到服务. 起初用service时,我便把service和factory()理所当然地关联起来了. 确 ...
- 第二节Unity3D开发环境安装(windows系统)
这一节准备安装开发环境. 1. 首先先下载软件包:http://pan.baidu.com/s/1imYVv 4.2版本2.下载完后,解压会看到两个文件(运行第二个安装包) 3.准备安装,这 ...
- centos 6.5下安装mysql+nginx+redmine 3.1.0 笔记
centos 6.5下安装mysql+nginx+redmine 3.1.0 笔记 目录[-] 过程 1.安装RVM 2.利用rvm安装 Ruby 1.9.3 并设为默认 3.安装rails 4.安装 ...
- WebView与JavaScript的交互
目录: 一.整体思路 二.简单例子实现过程 1.打开项目的asset目录,创建新的文件test.html 2.补充html代码:添加供本地调用的js方法.调用本地方法的js ...
- Java算法-各种题目总结
1.排列计算 /*[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子 ...
- sphinx在c#.net平台下使用(一)
Sphinx是由俄罗斯人Andrew Aksyonoff开发的一个可以结合MySQL,PostgreSQL全文检索引擎.意图为其他应用提供高速.低空间占用.高结果 相关度的全文搜索功能.是做站内全文搜 ...
- github 建立博客
Last login: Wed Jan 27 20:33:21 on console liukun-MBP:~ kamil$ cd ~/.ssh/ liukun-MBP:.ssh kamil$ ls ...