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

1:中序和后序遍历构成一棵树。2:採用递归的方法。3:把两个数组分别分成两部分;4:注意递归结束情况

    TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
if(inorder.size() == 0 || postorder.size() == 0 || inorder.size() != postorder.size())
{
return NULL;
} int size = (int)inorder.size();
return buildTreeCore(inorder, 0, postorder, 0, size);
} TreeNode *buildTreeCore(vector<int> &inorder, int inStart, vector<int> &postorder, int postStart, int length)
{
if(length == 1)
{
if(inorder[inStart] != postorder[postStart])
{
return NULL;
}
} int rootValue = postorder[postStart + length - 1];
TreeNode *root = new TreeNode(rootValue); int i = 0;
for(; i < length; i++)
{
if(inorder[inStart + i] == rootValue)
{
break;
}
} if(i == length)
{
return NULL;
} if(i > 0)
{
root->left = buildTreeCore(inorder, inStart, postorder, postStart, i);
} if(i < length - 1)
{
root->right = buildTreeCore(inorder, inStart + i + 1, postorder, postStart + i, length - i - 1);
} return root;
}

73_leetcode_Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

  1. Construct Binary Tree from Inorder and Postorder Traversal

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

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

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

  4. 【题解二连发】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 ...

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

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

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

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

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

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

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

  9. LeetCode_Construct Binary Tree from Inorder and Postorder Traversal

    一.题目 Construct Binary Tree from Inorder and Postorder Traversal My Submissions Given inorder and pos ...

随机推荐

  1. MFC的最大化,最小化,关闭

    最大化.最小化和关闭按钮是窗口中最主要的元素.首先要说明,说他们是按钮其实是不准确的,按钮是一种窗口,而这三个组件根本就不是窗口,而是一个窗口常见的组成部分.出于习惯的原因,这里还是称呼他们为按钮. ...

  2. cocos2dx进阶学习之CCAction

    CCAction在cocos2dx中,抽象了节点的动作.它由CCObject派生,因此它不是渲染节点. 它经常的用法是,创建一个action,然后让某个CCNode对象(一般是精灵),去执行它. 在马 ...

  3. 数据解析之XML和JSON

    1. 解析的基本的概念 解析:从事先规定好的格式中提取数据 解析前提:提前约定好格式,数据提供方按照格式提供数据.数据获取方则按照格式获取数据 iOS开发常见的解析:XML解析.JOSN解析 2. X ...

  4. error: property&#39;s synthesized getter follows Cocoa naming convention for returning &#39;owned&#39; objects

    出现这样的情况,主要是属性名中包括  keyword. You can solve this by: Renaming that property: @property (strong, nonato ...

  5. [转]如何在本地安装 Homebrew

    作者:shede333 主页:http://my.oschina.net/shede333  官网:http://brew.sh/index_zh-cn.html 安装方式见 官网,在shell里执行 ...

  6. mysqli_set_charset和SET NAMES优劣分析

    bool mysqli_set_charset ( mysqli $link , string $charset ) 这应该是首选的用于改变字符编码的方法,不建议使用 mysqli_query()执行 ...

  7. 单实例支撑每天上亿个请求的SSDB

    SSDB 是一个 C++ 开发的 NoSQL 存储服务器, 支持 zset, map 数据结构, 可替代 Redis, 特别适合存储集合数据. SSDB 被开发和开源出来后, 已经在生产环境经受了3个 ...

  8. zk leader选举自动完成

    server 1: [root@wx03 bin]# ./zkServer.sh status ZooKeeper JMX enabled by default Using config: /zook ...

  9. 【深圳,武汉】一加科技(One Plus)招聘,寻找不...

    [深圳,武汉]一加科技(One Plus)招聘,寻找不... [深圳,武汉]一加科技(One Plus)招聘,寻找不... 来自: 一加 2013-12-30 15:28:04         标题: ...

  10. 最简单也最难——如何获取到Android控件的高度

    问题 如何获取一个控件的长和高,相信很多朋友第一眼看见这个问题都会觉得很简单,直接在onCreate里面调用getWidth.getMeasuredWidth不就可以获得了吗,但是,事实上是并没有简单 ...