题目:

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. 利用Jquery处理跨域请求

    在项目制作过程中,可能会用到ajax来提高用户体验,这里终于研究出来,利用jquery来进行跨域请求,在用$.getJSON这个方法时,前台页面中需这样写 $.getJSON(“需要提交处理的url? ...

  2. 各种数据处理方案(SQL,NoSQL,其他)的应用场景

    综合stackoverflow和linkin上的相关讨论,还有我个人的工作经验:   Redis应用场景(大部分场景下memcache可以用Redis代替,所以不单独讨论) 线上业务,读写的高性能要求 ...

  3. nginx php 安装

    .选定源码目录选定目录 /data/klj/ cd /data/klj/ 2.安装PCRE库cd /data/klj/wget ftp://ftp.csx.cam.ac.uk/pub/software ...

  4. 2013/8/28 JS+HTML 三级省市区联动

    var mp = ["安徽","北京","福建","甘肃","广东","广西", ...

  5. dedecms 根据key取得联动类型(enum)值

    ---恢复内容开始--- //$key:如城市的ID,$enum_file  data/enums中的文件 function get_enum_data($key, $enum_file)    {  ...

  6. Delphi For Android 开发笔记 1 - 开发工具介绍

    在开始前,推荐喜欢delphi或者pascal的朋友,如果想将原来Windows的软件工程移植到Android,可使用CodeTyphon+Delphi XE7进行开发. 1.CodeTyphon C ...

  7. JS实现浏览器的title闪烁

    经常可以看见的title里面的消息提示,下面是JS的一种实现方法:主要是通过setTimeout方法设置一个定时器,切换消息提示,从而达title到消息提示的闪烁. <html> < ...

  8. 【微网站开发】之微信内置浏览器API使用

    最近在写微网站,发现了微信内置浏览器的很多不称心的地方: 1.安卓版的微信内浏览器底部总是出现一个刷新.前进.后退的底部栏,宽度很大,导致屏幕显示尺寸被压缩 2.分享当前网站至朋友圈时,分享的图片一般 ...

  9. NOJ1018-深度遍历二叉树

    题目要求很简单,前中后序遍历一棵二叉树.坑爹的是这道题的输入数据和测试数据压根不一样,找了好久原因,去讨论区看见有别人发的测试样例,修改了一下就AC了 测试样例是这个:DEH##FJ##G#CK### ...

  10. c/c++常用代码--udp多播

    #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <winsock.h ...