题目:

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. ADO.NET中的DataReader详解

    使用特性 原理图 PS:Read()使指针下移,同时销毁上一条.所以SqlDataReader是只进的. GetValue()是找当前行中的列 SqlDataReader()特性. 1)只进的  上面 ...

  2. MySQL连接语法

    http://www.cnblogs.com/hanzhaoxin/p/3590642.html 内连接:INNER  JOIN 内连接为 两个表中必须都同时满足条件 内连接,即最常见的等值连接自然连 ...

  3. Clearing Search Values

      Use the SearchClear() method which is part of the Field class to clear search values on a particul ...

  4. VS2010在非IE浏览器下调试Silverlight程序

    以Chrome为例: 第一步:在程序中设置断点. 第二步:右键点击web应用程序的起始页(.html或.aspx文件),选择"浏览方式",选中Chrome或其它非IE浏览器,点&q ...

  5. ES2015 ——let命令的暂时性死区

    ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. 和var不同的还有,let命令不存在变量提升,所以声明前调用变量,都会报错,这就涉及到 ...

  6. scan的filter使用

    本次操作的hbase的t1表的数据是: hbase(main)::> scan 't1' ROW COLUMN+CELL column=f1:age, timestamp=, value= co ...

  7. javascript变量和对象要注意的点

    js对象和变量,对象本身是没有名称的,之所以使用变量是为了通过某个名称来称呼这样一种不具名称的对象.变量分基本变量和引用类型变量.将基本类型的的值付给变量的话,变量将这个值本身保存起来. <sc ...

  8. Nginx安装第二步手动下载依赖包

    nginx可以使用各平台的默认包来安装,本文是介绍使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g++ 开发库之类的需要提前装好,这里默认你已经装好. ububtu平台编译环 ...

  9. JS调用腾讯接口获取天气

    想做个直接通过JS获取某个城市的天气.本来想通过直接调用中国气象网的接口: http://www.weather.com.cn/weather/101070201.shtml,但是跨域问题一直无法解决 ...

  10. 修改后的SQL分页存储过程,利用2分法,支持排序

    /****** Object: StoredProcedure [dbo].[sys_Page_v3] Script Date: 08/13/2014 09:32:28 ******/ SET ANS ...