题目:

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

说明:

1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进

算法思想

中序序列:C、B、E、D、F、A、H、G、J、I
 
后序序列:C、E、F、D、B、H、J、I、G、A
 
递归思路:
  1. 根据后序遍历的特点,知道后序遍历最后一个节点为根节点,即为A
  2. 观察中序遍历,A左侧CBEDF为A左子树节点,A后侧HGJI为A右子树节点
  3. 然后递归的构建A的左子树和后子树

实现:

 /**
* 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> &inorder, vector<int> &postorder) {
return creatTree(inorder.begin(),inorder.end(),postorder.begin(),postorder.end());
}
private:
template<typename InputIterator>
TreeNode *creatTree(InputIterator in_beg,InputIterator in_end,InputIterator post_beg,InputIterator post_end)
{
if(in_beg==in_end||post_beg==post_end) return nullptr; //空树
TreeNode *root=new TreeNode(*(post_end-));
auto inRootPos=find(in_beg,in_end,root->val);//中序遍历中找到根节点,返回迭代指针
int leftlen=distance(in_beg,inRootPos);//中序遍历起点指针与找到的根节点指针的距离
root->left=creatTree(in_beg,inRootPos,post_beg,next(post_beg,leftlen));//递归构建左子数
root->right=creatTree(next(inRootPos),in_end,next(post_beg,leftlen),post_end-);//递归构建右子树
return root;
}
};

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)的更多相关文章

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

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

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

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

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

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

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

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

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

随机推荐

  1. hihoCoder 1403 后缀数组一·重复旋律(后缀数组+单调队列)

    #1403 : 后缀数组一·重复旋律 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构成 ...

  2. SPOJ 1825 Free Tour | 终极之树分治

    求树上最长路径使得经过的拥挤节点个数不超过K //欢迎访问这个博客!http://www.cnblogs.com/luyouqi233/p/8036828.html #include<cstdi ...

  3. BZOJ1855 [Scoi2010]股票交易 【单调队列优化dp】

    题目链接 BZOJ1855 题解 设\(f[i][j]\)表示第\(i\)天结束时拥有\(j\)张股票时的最大收益 若\(i \le W\),显然在这之前不可能有交易 \[f[i][j] = max\ ...

  4. POJ3680:Intervals(离散化+最大流最小费用)

    Intervals Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9320   Accepted: 4014 题目链接:ht ...

  5. openGL初学函数解释汇总

    openGL初学函数解释汇总 1.GLUT工具包提供的函数 //GLUT工具包所提供的函数 glutInit(&argc, argv);//对GLUT进行初始化,这个函数必须在其它的GLUT使 ...

  6. InnoDB的多版本并发控制(MMVC)

    InnoDB的MVCC之(乐观锁),是通过在每行记录保存两个隐藏列来实现的.这两个列,一个是存创建时间,一个是删除时间,这里的时间指的是,系统版本号,并不是真正的时间值. 每开始一个新的事务,系统版本 ...

  7. pycharm远程登录mysql

    pycharm远程登录mysqlmysql远程登录需要修改配置文件:cd /etc/mysql/mysql.conf.d/sudo vim mysqld.cn修改bing-address=0.0.0. ...

  8. UVA 306 Cipher

    题意 :lucky cat里有翻译.英文也比较好懂. 很容易发现有周期然后就拍就好了 注意每组数据后边都有空行 包括最后一组.一开始以为最后一组没有空行.唉.. #include <map> ...

  9. shell脚本查看服务器基本信息

    #!/bin/sh #电脑概览 #电脑型号 ComputerModel=`/usr/bin/sudo /usr/sbin/dmidecode | grep -A2 "System Infor ...

  10. Visual Studio跨平台开发(4):Xamarin Android控制项介绍

    前言 不同于iOS, Xamarin 在Visual Studio中针对Android, 可以直接设计使用者界面. 在本篇教学文章中, 笔者会针对Android的专案目录结构以及基本控制项进行介绍, ...