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. Python基础——判断和循环

    判断 缩进代替大括号. 冒号(:)后换号缩进. if test=100 if test>50: print('OK') print('test') if-elif-else test=50 if ...

  2. STM32F407VET6之IAR之ewarm7.80.4工程建立(基于官方固件库1.6版本) 的工程文件目录

    最后整理结构如下所示,├─cmsis│ startup_stm32f401xx.s│ startup_stm32f40xx.s│ startup_stm32f40_41xxx.s│ startup_s ...

  3. ubuntu12.04ppa安装emacs24

    ppa地址:https://launchpad.net/~cassou/+archive/emacs 因为debian版本的emacs-snapshot维护者停止更新,所有ubuntu上的也停止了. ...

  4. linux学习-用户的特殊 shell 与 PAM 模块

    特殊的 shell, /sbin/nologin 『无法登入』指的是:『这个使用者无法使用 bash 或其他 shell 来登入系统』而已, 并不是说这个账号就无法使用其他的系统资源! 让某个具有 / ...

  5. 重新造轮子之静态链接1(Static linking)

    最近学习计算机病毒学的过程中,又讲到了静态链接的问题,联想到了之前保健哥在信息安全的课堂上向我们展示了一个没有main()函数的C程序到底应该如何编写.个人觉得这个小实验对于加深静态链接的过程的理解也 ...

  6. HDU 5527 Too Rich 贪心

    题意: 有\(10\)种面值为\(1, 5, 10, 20, 50, 100, 200, 500, 1000, 2000\)的纸币,现在你要选最多的数量凑成\(p\)块钱. 分析: 同样分析问题的反面 ...

  7. 网页静态化解决方案Freemarker

    序言: 沉淀了三个月,逐步将自己最近两年在公司中用到的技术和知识点,重新整理归纳了下,对比以前可以发现,现在技术更新越来越快,也越来越成熟,在互联网企业,用到的技术也更先进,更领先,比如微服务.分布式 ...

  8. adb -a server nodaemon,设备一直显示 offline,而 adb devices 一直显示 device【已解决】

    1. adb -a server nodaemon 一直显示 offline 2. adb devices 一直显示 device 谷歌 和 度娘了一圈,未寻得解决办法 # 解决方法 问题已解决,使用 ...

  9. 转:模式窗口showModalDialog的用法总结

    模式窗口showModalDialog的用法总结   最近几天一直在处理模式窗口的问题,索性写了这篇总结,以供参考: 1.打开窗口:var handle = window.showModalDialo ...

  10. Python 单例模式(3种方式)

    方式一: # 单例模式: # 实现目的:实例化多次,得到的实例是同一个,就是同一个对象,同一个名称空间(更加节省空间) ####################################方式一: ...