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 duplicates do not exist in the tree.

根据定义,后序遍历postorder的最后一个元素为根。

由于元素不重复,通过根可以讲中序遍历inorder划分为左子树和右子树。

递归下去即可求解。

/**
* Definition for binary tree
* 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 Helper(inorder, , inorder.size()-, postorder, , postorder.size()-);
}
TreeNode* Helper(vector<int>& inorder, int begin1, int end1, vector<int>& postorder, int begin2, int end2)
{
if(begin1 > end1)
return NULL;
else if(begin1 == end1)
return new TreeNode(inorder[begin1]); TreeNode* root = new TreeNode(postorder[end2]);
int i = begin1;
for(; i <= end1; i ++)
{
if(inorder[i] == postorder[end2])
break;
}
int leftlen = i-begin1; root->left = Helper(inorder, begin1, begin1+leftlen-, postorder, begin2, begin2+leftlen-);
root->right = Helper(inorder, begin1+leftlen+, end1, postorder, begin2+leftlen, end2-);
return root;
}
};

【LeetCode】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 Traversall

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

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

  4. 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

  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 由中序和后序遍历建立二叉树

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

随机推荐

  1. iOS Contact框架功能研究

    兄弟们,直接复制,感谢度娘,感谢谷哥!!! 1.读取联系人通讯录 /** *  读取联系人通讯录 */ -(NSMutableArray*)getContactList{ NSMutableArray ...

  2. MariaDB 主从复制

    MySQL Replication:NySQL复制,MySQL的复制默认为异步工作模式    mysql的复制功能是mysql内置的,装上它之后就具备了这个功能,而mysql复制是mysql实现大规模 ...

  3. C#利用NPOI在同一个Excel文件中创建多个sheet

    借用NPOI来实现,要在同一Excel文件中创建多个sheet,只需要在同一个workbook中创建多个sheet即可.要注意的是,sheet的名字一定不能重复.下面是实现的代码: private v ...

  4. Tiny microcontroller hosts dual dc/dc-boost converters

    Batteries are the typical power sources for portable-system applications, and it is not unusual thes ...

  5. FreeRTOS Memory Management ( IAR )

    http://www.freertos.org/a00111.html The RTOS kernel allocates RAM each time a task, queue, mutex, so ...

  6. Valera and Fruits

    B. Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. 数据库连接池中是将connection放进threadlocal里的

    我有几点不太明白的,望各位大侠指教下.1.j2ee的应用中,有一个用户请求就会启动一个线程.而如果我们把connection放在Threadlocal里的话,那么我们的程序只需要一个connectio ...

  8. dao层知识点总结

    1.dao层要有connection 2.dao层进行分页,mysql limit关键字 3.dao层进行结果集转换为java bean 4.dao层queryforlist

  9. linux 程序移植到Android

    用动态链接的方法: arm-linux-gcc hello.c -o hello.out -Wl,-dynamic-linker=/system/lib/ld-linux.so.3 并且拷贝文件到安卓 ...

  10. 数学图形(2.20)3D曲线

    这一节主要是发布我自己写的3D曲线, (1)立体flower线圈 vertices = a = 10.1 b = 3.1 s = (a + b) / b o = i = to (**PI) j = m ...