LeetCode OJ--Construct Binary Tree from Inorder and Postorder Traversal *
http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
知道二叉树的中序遍历和后序遍历序列,求二叉树。
使用递归
#include <iostream>
#include <vector>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution{
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder)
{
return buildTree(begin(inorder),end(inorder),begin(postorder),end(postorder));
}
template<typename BidiIt>
TreeNode* buildTree(BidiIt in_first, BidiIt in_last,BidiIt post_first,BidiIt post_last)
{
if(in_first ==in_last)
return nullptr;
if(post_first == post_last)
return nullptr;
const auto val = *prev(post_last);
TreeNode* root = new TreeNode(val); auto in_root_pos = find(in_first,in_last,val);
auto left_size = distance(in_first,in_root_pos);
auto post_left_last = next(post_first,left_size); root->left = buildTree(in_first,in_root_pos,post_first,post_left_last);
root->right = buildTree(next(in_root_pos),in_last,post_left_last,prev(post_last));
return root;
}
}; int main()
{
Solution myS;
int arr1[] = {,};//,5,1,6,3,7 };
int arr2[] = {,};//,2,6,7,3,1 };
vector<int> inorder(arr1,arr1+) ;
vector<int> postorder(arr2 ,arr2+);
TreeNode *myNode; myNode = myS.buildTree(inorder,postorder); cout<<"hi"<<endl;
return ;
}
LeetCode OJ--Construct Binary Tree from Inorder and Postorder Traversal *的更多相关文章
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
		Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ... 
- 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 ... 
- (二叉树 递归) 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 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 Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)
		Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ... 
- C#解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 Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++
		Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ... 
- 【leetcode】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[105] Construct Binary Tree from Inorder and Postorder Traversal
		代码实现:给定一个中序遍历和后序遍历怎么构造出这颗树!(假定树中没有重复的数字) 因为没有规定是左小右大的树,所以我们随意画一颗数,来进行判断应该是满足题意的. 3 / \ 2 4 /\ / \1 6 ... 
随机推荐
- 【NOIP提高A组模拟2018.8.14】 区间
			区间加:差分数组修改 O(n)扫描,负数位置单调不减 #include<iostream> #include<cstring> #include<cstdio> # ... 
- Java使用ResourceBundle类读取properties文件中文乱码的解决方案
			Java使用java.util.ResourceBundle类的方式来读取properties文件时不支持中文,要想支持中文必须将文件设置为ISO-8859-1编码格式,这对于开发工具默认为UTF-8 ... 
- nginx的url重写
			1.1 简介 url重写由ngx_http_rewrite_module模块提供,默认会安装,但该模块功能的实现需要pcre.URL重写技术不仅要求掌握几个指令的语法.熟悉简单的正则表达式,还需要尽量 ... 
- Vue之数据传递
			基础:vue的响应式规则 简单的props更新 父组件 <template> <div> <block-a :out-data="x">< ... 
- DC 课程内容
- nginx静态资源web服务
			静态资源:非服务器动态运行生成的文件 浏览器端渲染:html ,css,js 图片:jpeg,gif,png 视频:flv ,mpeg 文件:txt,等任意下载文件 静态资源服务场景:CDN 文件读取 ... 
- 04 Django模板
			基本概念 作为Web框架,Django提供了模板,用于编写html代码,还可以嵌入模板代码更快更方便的完成页面开发,再通过在视图中渲染模板,将生成最终的html字符串返回给客户端浏览器 模版致力于表达 ... 
- Ubuntu系统里的python
			Ubuntu系统里,默认安装python2.7.x版本的python,直接执行python命令,打开的将是python 2.7.x版本:python3版本的需要自行安装,安装成功后,执行python3 ... 
- nw335 debian sid x86-64 -- 1 需求介绍
			自己的台式机上面有有线网卡,路由器在客厅,托一条长长的线,关门也不方便.没有选择PCI无线网卡,没有选择nano类型的迷你网卡.买了nw335,带一条5DB天线,信号应该会好点.于是,开始了在debi ... 
- 更改activity切换方式
			overridePendingTransition(enterAnim, exitAnim); Intent intent =new Intent(this,item2.class); startAc ... 
