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

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

========

利用:中序+后序遍历

====

code:

/**
* 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:
///
template<typename Iterator>
TreeNode *help_buildTree_ip(Iterator in_first,Iterator in_last,
Iterator post_first,Iterator post_last){
if(in_first == in_last) return nullptr;
if(post_first == post_last) return nullptr; auto val = *prev(post_last);
TreeNode *root = new TreeNode(val); auto in_root_pos = find(in_first,in_last,val);
auto left_size = distance(in_first,in_root_pos);
auto post_left_last = next(post_first,left_size); root->left = help_buildTree_ip(in_first,in_root_pos,post_first,post_left_last);
root->right = help_buildTree_ip(next(in_root_pos),in_last,post_left_last,prev(post_last)); return root;
}
TreeNode* buildTree_ip(vector<int> &inorder,vector<int> &postorder){
return help_buildTree_ip(inorder.begin(),inorder.end(),postorder.begin(),postorder.end());
}
};

106. Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

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

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

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

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

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

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

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  5. C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

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

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...

  7. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  8. (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  9. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

随机推荐

  1. POJ 1321 棋盘问题 --- DFS

    POJ 1321 题目大意:给定一棋盘,在其棋盘区域放置棋子,需保证每行每列都只有一颗棋子. (注意 .不可放 #可放) 解题思路:利用DFS,从第一行开始依次往下遍历,列是否已经放置棋子用一个数组标 ...

  2. Android——ListView相关作业(修改版)

    给GridView提供点击按钮添加新数据,单击项目修改,长按删除功能 activity_practise7的layout文件: <?xml version="1.0" enc ...

  3. 利用Web服务器网络打洞

    好了有些标题党了.这里想说的是:某些网络,除了http 80服务,其它端口的服务都被限制了,这个时候可以用http web服务器来进行代理转发. 以Apache为例,支持ssh登录到其它服务器的配置如 ...

  4. JS原型链原理(链表)

      任何一个对象都有一个prototype的属性,在js中可以把它记为:__proto__   当初ECMAscript的发明者为了简化这门语言,同时又保持继承的属性,于是就设计了这个链表..在数据结 ...

  5. 如何读懂 Intel HEX 文件

    什么是 Intel HEX 文件格式   转自:http://www.cnblogs.com/imapla/archive/2013/03/16/2926133.htmlIntel HEX 文件是遵循 ...

  6. android:id="@+id/button1" 与 android:id="@id/button1" 区别 @string

    一.android:id="@+id/button1" 与 android:id="@id/button1" 区别 android:id="@+id/ ...

  7. java.util.concurrent Class ThreadPoolExecutor

    http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html

  8. int(M)与int

    int(M) ,加上zerofill后M才表现出有点效果,比如 int(3) zerofill,你插入到数据库里的是10,则实际插入为010,也就是在前面补充加了一个0.如果int(3)和int(10 ...

  9. Nginx重写规则指南 转

    http://www.ttlsa.com/nginx/nginx-rewriting-rules-guide/ Nginx重写规则指南 当运维遇到要重写情况时,往往是要程序员把重写规则写好后,发给你, ...

  10. http,ftp

    HTTP和FTP的区别 标签: ftpFTPhttpHttpHTTP 2012-12-13 19:51 10544人阅读 评论(1) 收藏 举报  分类: 网络(2)    目录(?)[+]   今天 ...