题目:

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. 通过Migration在EF6中用多个DbContext

    通过Migration在EF6中用多个DbContext EF EF6 C# Migration 通过Migration在EF6中用多个DbContext 前言 实现目标 设置多数据上下文 更新数据脚 ...

  2. 实现Java JTable的应用案例

    代码如下 import Java.awt.Component; import java.awt.Dimension; import java.awt.FontMetrics; import javax ...

  3. .NET中的属性

    1.What?什么是属性       属性是对字段的封装.当类中有了一个字段以后,为了控制这个字段对外的一些表现(例如可访问性,是只读?只写?或者对自读赋值做一些必要的验证等等)我们把这个字段私有化( ...

  4. %SELECTALL

    If you ever need to create a view that selects all fields from a particular record, then you should ...

  5. linux 常用命令及技巧

    linux 常用命令及技巧 linux 常用命令及技巧:linux 常用命令总结: 一. 通用命令: 1. date :print or set the system date and time 2. ...

  6. PHP-PCRE正则表达式函数

    PCRE正则表达式函数 PCRE字符类 \\b        词边界 \\d        匹配任意数字 \\s        匹配任意空白,如TAB制表符或空格 \\t        匹配一个TAB ...

  7. 用户View,五大布局

    1.LinearLayout 线性布局 android:orientation="horizontal" 制定线性布局的排列方式 水平 horizontal 垂直 vertical ...

  8. 消息队列之RabbitMQ

    参考教程: http://www.rabbitmq.com/getstarted.html http://www.cnblogs.com/shanyou/p/4067250.html http://w ...

  9. ado.net的5个主要对象

    connection 连接对象 command 命令对象,指示要执行的命令和存储过程! datareader是一个向前的只读的数据流. dataadapter是功能强大的适陪器,支持增删改查的功能 d ...

  10. eclipse新建android项目,编译出错解决方法

    1.新建android项目 2.在libs中,将android-support-v4.jar添加到生成目录 3.如果项目引用了ActionBar等,需要引用V7的话,添加外部Jar包,路径为eclip ...