Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树
题意:根据二叉树的中序遍历和后序遍历恢复二叉树。
解题思路:看到树首先想到要用递归来解题。以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序遍历为{4,5,2,6,7,3,1},我们可以反推回去。由于后序遍历的最后一个节点就是树的根。也就是root=1,然后我们在中序遍历中搜索1,可以看到中序遍历的第四个数是1,也就是root。根据中序遍历的定义,1左边的数{4,2,5}就是左子树的中序遍历,1右边的数{6,3,7}就是右子树的中序遍历。而对于后序遍历来讲,一定是先后序遍历完左子树,再后序遍历完右子树,最后遍历根。于是可以推出:{4,5,2}就是左子树的后序遍历,{6,3,7}就是右子树的后序遍历。而我们已经知道{4,2,5}就是左子树的中序遍历,{6,3,7}就是右子树的中序遍历。再进行递归就可以解决问题了。
/**
* 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) {
if(inorder.size()!=postorder.size()||inorder.size()<)
return NULL;
return build(inorder,postorder,,inorder.size()-,,postorder.size()-);
}
TreeNode *build(vector<int> &inorder, vector<int> &postorder, int startin,int endin,int startpost,int endpost){
if(startin>endin||startpost>endpost) return NULL;
if(startin==endin) return new TreeNode(inorder[startin]);
TreeNode *res = new TreeNode(postorder[endpost]);
int tmp=postorder[endpost];
int index=;
while((startin+index)<=endin){
if(inorder[startin+index]==tmp)
break;
index++;
}
res->left=build(inorder,postorder,startin,startin+index-,startpost,startpost+index-);
res->right=build(inorder,postorder,startin+index+,endin,startpost+index,endpost-);
return res;
}
};
Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树的更多相关文章
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- 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 ...
- [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 ...
- Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)
根据中序和后续遍历构建二叉树. /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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 ...
- 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 ...
- 【题解二连发】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 ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- bootstrap里的fileimput的小问题
fileinput 是bootstrap 里面一个非常好的插件 于是我很开心的开始的使用了 $("#file_upload").fileinput({ uploadUrl: &qu ...
- [python学习篇][书籍学习][python standrad library][内建类型]迭代器类型
我们已经知道,可以直接作用于for循环的数据类型有以下几种:一类是集合数据类型,如list.tuple.dict.set.str等:一类是generator,包括生成器和带yield的generato ...
- 以前刷过的数位dp
TOJ1688: Round Numbers Description The cows, as you know, have no fingers or thumbs and thus are una ...
- 【bzoj4260】Codechef REBXOR Trie树
题目描述 输入 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. 输出 输出一行包含给定表达式可能的最大值. 样例输入 5 1 2 3 1 2 样例输出 ...
- 【Luogu】P2445动物园(最大流)
题目链接 题目本身还是比较水的吧……容易发现是最大流套上dinic跑一遍就好了,并不会超时. 比较不偷税的一点是关于某动物的所有目击报告都符合才能连边……qwqqwqqwq #include<c ...
- [CQOI2010] 扑克牌 (二分答案,巧解)
Description 你有n种牌,第i种牌的数目为ci.另外有一种特殊的牌:joker,它的数目是m.你可以用每种牌各一张来组成一套牌,也可以用一张joker和除了某一种牌以外的其他牌各一张组成1套 ...
- IE8,11的iframe高度自适应
兼容模式:function iFrameHeightTzinfo() { var ifm= document.getElementById("iframe_tzinfo"); // ...
- 数据库操作——pymysql模块
一 import pymysql conn=pymysql.connect( host='localhost', port=3306, user='zuo', password=', database ...
- 用jquery写的position瀑布流布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- linux sleep函数
应用程序: #include usleep(n) //n微秒 Sleep(n)//n毫秒 sleep(n)//n秒 驱动程序: #include mdelay(n) //milliseconds 其实 ...