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 *的更多相关文章

  1. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

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

  3. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

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

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

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

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

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

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

  10. leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal

    代码实现:给定一个中序遍历和后序遍历怎么构造出这颗树!(假定树中没有重复的数字) 因为没有规定是左小右大的树,所以我们随意画一颗数,来进行判断应该是满足题意的. 3 / \ 2 4 /\ / \1 6 ...

随机推荐

  1. node 文件下载到本地 (支持中文文件名)

    downloadfile:function(req,res,next){ var name= encodeURI(req.query.name); var path= req.query.url; v ...

  2. NodeJS基础API-path相关的问题basename,extname,dirname,parse,format,sep,delimiter,win32,posix

    path 参考文档:http://nodejs.cn/api/path.html const {normalize} = require('path'); // ES6语法 // 相当于 const ...

  3. 04Windows中的字符类型

    1. Windows 中常用的数据类型定义 //WinNt.h中定义 typedef unsigned short wchar_t; //A 16-bit character typedef char ...

  4. 【mac】【转发】Mac系统升级后,按大小写键没反应了,切换大小写的灯不亮了

    Mac系统升级后发现caps lock 锁定大小写的键,失灵了,居然可以用来切换输入法了,经过一排查后, 使用以下几种方法处理: 方式一:长按 caps lock 键,来切换大小写 方式二:caps ...

  5. Applied Nonparametric Statistics-lec6

    Ref: https://onlinecourses.science.psu.edu/stat464/print/book/export/html/8 前面都是对一两个样本的检查,现在考虑k个样本的情 ...

  6. 实验4 —— [bx]和loop的使用

    实验 综合使用 loop.[bx],编写完整汇编程序,实现向内存 b800:07b8 开始的连续 16 个字单元重复填充字数据 0403H. 以下为示例程序: assume cs:code # 1 c ...

  7. 用\r做出进度条

    在做ftp作业的时候,需要做一个上传和下载的进度条,做的时候发现用\r很容易就能做出来 def show_progress(self, has, total): rate = float(has) / ...

  8. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  9. 链表中倒数第k个结点 【微软面试100题 第十三题】

    题目要求: 输入一个链表,输出该链表中倒数第k个结点.链表的倒数第0个结点为链表的尾指针. 参考资料:剑指offer第15题 题目分析: 1.两个指针,第一个先走k步,然后两个指针同时走,直到第一个走 ...

  10. iOS 9下支持的键盘类型:

    http://blog.csdn.net/cloudox_/article/details/50532124