题目:

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

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

代码:

/**
* Definition for a binary tree node.
* 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) {
if ( inorder.size()== || postorder.size()== ) return NULL;
return Solution::buildTreeIP(inorder, , inorder.size()-, postorder, , postorder.size()-);
}
static TreeNode* buildTreeIP(
vector<int>& inorder,
int bI, int eI,
vector<int>& postorder,
int bP, int eP )
{
if ( bI > eI ) return NULL;
TreeNode *root = new TreeNode(postorder[eP]);
int rootPosInorder = bI;
for ( int i = bI; i <= eI; ++i )
{
if ( inorder[i]==root->val ) { rootPosInorder=i; break; }
}
int leftSize = rootPosInorder - bI;
int rightSize = eI - rootPosInorder;
root->left = Solution::buildTreeIP(inorder, bI, rootPosInorder-, postorder, bP, bP+leftSize-);
root->right = Solution::buildTreeIP(inorder, rootPosInorder+, eI, postorder, eP-rightSize, eP-);
return root;
}
};

tips:

思路跟Preorder & Inorder一样。

这里要注意:

1. 算左子树和右子树长度时,要在inorder里面算

2. 左子树和右子树长度可能一样,也可能不一样;因此在计算root->left和root->right的时候,要注意如何切vector下标(之前一直当成左右树长度一样,debug了一段时间才AC)

==============================================

第二次过这道题,沿用了之前construct binary tree的思路,代码一次AC。

/**
* Definition for a binary tree node.
* 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 Solution::build(inorder, , inorder.size()-, postorder, , postorder.size()-);
}
TreeNode* build(
vector<int>& inorder, int bi, int ei,
vector<int>& postorder, int bp, int ep)
{
if ( bi>ei || bp>ep) return NULL;
TreeNode* root = new TreeNode(postorder[ep]);
int right_range = ei - Solution::findPos(inorder, bi, ei, postorder[ep]);
int left_range = ei - bi - right_range;
root->left = Solution::build(inorder, bi, ei-right_range-, postorder, bp, ep-right_range-);
root->right = Solution::build(inorder, bi+left_range+ , ei, postorder, bp+left_range, ep-);
return root;
}
int findPos(vector<int>& order, int begin, int end, int val)
{
for ( int i=begin; i<=end; ++i ) if (order[i]==val) return i;
}
};

【Construct Binary Tree from Inorder and Postorder Traversal】cpp的更多相关文章

  1. 【构建二叉树】02根据中序和后序序列构造二叉树【Construct Binary Tree from Inorder and Postorder Traversal】

    我们都知道,已知中序和后序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 中序遍历序列,           ======O=========== 后序遍 ...

  2. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  3. 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...

  4. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  5. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  6. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  7. 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...

  8. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

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

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

随机推荐

  1. openstack简介

    OpenStack是一个开源的云计算管理平台项目,由几个主要的组件组合起来完成具体工作.OpenStack支持几乎所有类型的云环境,项目目标是提供实施简单.可大规模扩展.丰富.标准统一的云计算管理平台 ...

  2. 移植u-boot-1.1.6之mtdparts分区

    和u-boot高版本不同,mtdparts命令没有cmd_mtdparts这么一个单独的文件来实现. 不过,搜索uboot可以在cmd_jffs2.c里面看到如下代码: U_BOOT_CMD( mtd ...

  3. Win2008R2PHP5.4环境加载Zend模块

    1.需要2个文件 Zend Optimizer和Zend Guard Loade s 下载Zend Guard Loader包.(官方地址:http://www.zend.com/en/product ...

  4. c#中判断对象为空的几种方式(字符串等)

    (1)先了解几个与空类型相关的关键字和对象  Null : 关键字表示不引用任何对象的空引用,它是所有引用类型变量的默认值,在2.0版本之前也就只有引用变量类型可以为null,如(string a=n ...

  5. [原]打造Python开发环境之Python环境

    人生苦短,我用Python 一.升级到python2.7 开发环境的系统是centos 6.0, 默认的python版本是2.6.6, 由于线上环境是python2.7,为了防止版本差异产生的问题,所 ...

  6. mariadb的explain分析及InnoDB存储引擎

    id: 当前查询语句中,每个SELECT语句的编号,     id: 1  表示简单类型的查询 复杂类型的查询有三种:简单子查询,用于FROM中的子查询,联合查询:UNION 注意:UNION查询的分 ...

  7. PHP数组在HTML之中的应用

    <select name="data[status]" id="" <?php if(in_array($list['status'],array( ...

  8. asp.net导出excel示例代码

    asp.net导出excel的简单方法. excel的操作,最常用的就是导出和导入. 本例使用NPOI实现. 代码:/// <summary> );             ;       ...

  9. (转)Arcgis API常用接口调用方法

    var map, navToolbar, editToolbar, tileLayer, toolbar;//var mapBaseUrl = "http://localhost:8399/ ...

  10. 【转】MYISAM表批量压缩

    关于对MYISAM表的压缩,可以使用myisampack和myisamchk完成(myisampack完之后必须进行myisamchk才能使用压缩后的表,而且是只读的), 其详细地用法可以参考官方文档 ...