Construct Binary Tree from Inorder and Postorder Traversal (&&Preorder and Inorder 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.
这道题之前算法课上好像遇到过,思路也很简单的。
思路:后序序列的最后一个元素就是树根,然后在中序序列中找到这个元素(由于题目保证没有相同的元素,因此可以唯一找到),中序序列中这个元素的左边就是左子树的中序,右边就是右子树的中序,然后根据刚才中序序列中左右子树的元素个数可以在后序序列中找到左右子树的后序序列,然后递归的求解即可。(在去除了根节点之后,中序遍历和后序遍历的前N个树都是左子树,有了这个前提之后,代码也就好写了。)
特别注意的是:之前提到过,每当涉及到树,就应该考虑到递归能不能用。
/**
* 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:
typedef vector<int>::iterator Iter;
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
return buildTreeRecur(inorder.begin(), inorder.end(), postorder.begin(), postorder.end());
}
TreeNode *buildTreeRecur(Iter istart, Iter iend, Iter pstart, Iter pend)
{
if(istart == iend)return NULL;
int rootval = *(pend-);
Iter iterroot = find(istart, iend, rootval);
TreeNode *res = new TreeNode(rootval);
res->left = buildTreeRecur(istart, iterroot, pstart, pstart+(iterroot-istart));
res->right = buildTreeRecur(iterroot+, iend, pstart+(iterroot-istart), pend-);
return res;
}
};
Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder 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:
typedef vector<int>::iterator Iter;
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
return buildTreeRecur(inorder.begin(), inorder.end(), preorder.begin(), preorder.end());
}
TreeNode *buildTreeRecur(Iter istart, Iter iend, Iter pstart, Iter pend)
{
if(istart == iend)return NULL;
int rootval = *pstart;
Iter iterroot = find(istart, iend, rootval);
TreeNode *res = new TreeNode(rootval);
res->left = buildTreeRecur(istart, iterroot, pstart+, pstart++(iterroot-istart));
res->right = buildTreeRecur(iterroot+, iend, pstart++(iterroot-istart), pend);
return res;
}
};
Construct Binary Tree from Inorder and Postorder Traversal (&&Preorder and Inorder Traversal )——数据结构和算法的基本问题的更多相关文章
- 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 ...
- 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】cpp
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...
- (二叉树 递归) 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 & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
- [LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- C++11——引入的新关键字
1.auto auto是旧关键字,在C++11之前,auto用来声明自动变量,表明变量存储在栈,很少使用.在C++11中被赋予了新的含义和作用,用于类型推断. auto关键字主要有两种用途:一是在变量 ...
- Linux 查询命令
which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查询文件名称 (find也 ...
- bzoj5118: Fib数列2(费马小定理+矩阵快速幂)
题目大意:求$fib(2^n)$ 就是求fib矩阵的(2^n)次方%p,p是质数,根据费马小定理有 注意因为模数比较大会爆LL,得写快速乘法... #include<bits/stdc++.h& ...
- Struts初探(二)
总是找不到对应的action,但别的没用到动态方法调用的都没有问题. 报异常:java.lang.reflect.InvocationTargetException - Class: com.open ...
- 使用Masonry在UIScrollView内布局
理论分析 首先,我们知道Autolayout改变了传统的以frame为主的布局思想.它其实是一种相对布局,核心思想是视图与视图之间的位置关系.比如,我们可以根据矩形的起始横坐标.纵坐标.长和宽这四个变 ...
- 洛谷P1937 [USACO10MAR]仓配置Barn Allocation
题目描述 Farmer John recently opened up a new barn and is now accepting stall allocation requests from t ...
- bnuoj53075 外挂使用拒绝
题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=53075 第一次给校赛出题,来为自己的题目写一发题解吧. 其实我原本的题意非常简洁: 结果被另一位 ...
- bzoj 2434 AC自动机+树状数组
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 3493 Solved: 1909[Submit][Sta ...
- Mac(Linux)上安装memcached步骤
Mac上安装memcached类似于在Linux平台上安装memcached. 主要需要做两块: 一.安装libevent库: 二.安装memcached; 一.安装libevent库 libeven ...
- DialogFragment 将数据传回Activity的onActivityResult方法
在MyActivity中 弹出一个DialogFragment (某一个控件的点击事件) search= findViewById(R.id.search); search.setOnClickLis ...