[itint5]根据前序后序遍历统计二叉树
这题有意思。一开始还想不清楚,看了解释,很棒。
这个题目的特殊之处是所有节点的值都是不一样的. 所以递归过程可以大大简化. 先看两种遍历的性质:
pre-order: root, left *************, right #########
post-order: **************left, ########right, root
所以 pre-order 的第一个元素一定等于 post-order 的最后一个元素. 然后在post-order中由前往后找, 找出等于pre-oder中第二个元素的位置, 也就是 left 的位置. 如果post-order中的这个位置不是倒数第二个, 说明左右子树都非空, 那么对左右子树递归调用后用乘法原理. 如果是倒数第二个, 说明有一个子树为空, return的值就是 2*递归调用非空子树.
int countHelper(vector<int>& preorder, vector<int>& postorder, int preA, int preB, int postA, int postB) {
if (preA > preB || postA > postB) return 0;
if (preA == preB && postA == postB && preorder[preA] == postorder[postB]) return 1;
// preB > preA && postB > postA
// assert(preorder[preA] == postorder[postB]
if (preorder[preA+1] == postorder[postB-1]) { // right tree or left tree is null
return 2 * countHelper(preorder, postorder, preA+1, preB, postA, postB-1);
} else { // right tree and left tree both exists
int rightInPreOrder = -1;
for (int i = preA+2; i < preorder.size(); i++) {
if (preorder[i] == postorder[postB-1]) {
rightInPreOrder = i;
break;
}
}
int leftInPostOrder = -1;
for (int i = postB-2; i >= 0; i--) {
if (postorder[i] == preorder[preA+1]) {
leftInPostOrder = i;
break;
}
}
return countHelper(preorder, postorder, preA+1, rightInPreOrder-1, postA, leftInPostOrder) *
countHelper(preorder, postorder, rightInPreOrder, preB, leftInPostOrder+1, postB-1);
}
}
int countValidTrees(vector<int>& preorder, vector<int>& postorder) {
// assert(preorder.size() == postorder.size());
return countHelper(preorder, postorder, 0, preorder.size()-1, 0, postorder.size()-1);
}
[itint5]根据前序后序遍历统计二叉树的更多相关文章
- [二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)
1119. Pre- and Post-order Traversals (30) Suppose that all the keys in a binary tree are distinct po ...
- 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 ...
- LintCode2016年8月8日算法比赛----中序遍历和后序遍历构造二叉树
中序遍历和后序遍历构造二叉树 题目描述 根据中序遍历和后序遍历构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下 ...
- Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树
题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序 ...
- [Swift]LeetCode889. 根据前序和后序遍历构造二叉树 | Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- (原)neuq oj 1022给定二叉树的前序遍历和后序遍历确定二叉树的个数
题目描述 众所周知,遍历一棵二叉树就是按某条搜索路径巡访其中每个结点,使得每个结点均被访问一次,而且仅被访问一次.最常使用的有三种遍历的方式: 1.前序遍历:若二叉树为空,则空操作:否则先访问根结点, ...
- 笔试算法题(36):寻找一棵二叉树中最远节点的距离 & 根据二叉树的前序和后序遍历重建二叉树
出题:求二叉树中距离最远的两个节点之间的距离,此处的距离定义为节点之间相隔的边数: 分析: 最远距离maxDis可能并不经过树的root节点,而树中的每一个节点都可能成为最远距离经过的子树的根节点:所 ...
- PAT-1119(Pre- and Post-order Traversals)+前序和后序遍历确定二叉树+判断二叉树是否唯一
Pre- and Post-order Traversals PAT-1119 这题难度较大,主要需要考虑如何实现根据前序遍历和后序遍历来确定一颗二叉树 一篇好的文章: 题解 import java. ...
- POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)
题目链接 问题描述 : We are all familiar with pre-order, in-order and post-order traversals of binary trees. ...
随机推荐
- krpano资料
- HTML5的Web SQL Database
本文将介绍 Web SQL Database 规范中定义的三个核心方法: openDatabase:这个方法使用现有数据库或新建数据库来创建数据库对象 transaction:这个方法允许我们根据情况 ...
- 【整理】c# 调用windows API(user32.dll)
User32.dll提供了很多可供调用的接口,大致如下(转自http://blog.csdn.net/zhang399401/article/details/6978803) using System ...
- CAF(C++ actor framework)使用随笔(unbecome与keep_behavior用法)
看usermanual(使用随笔一里面有)看到差不多一半的时候,这个keep_behavior与unbeacome的结合引起了我的注意.(这是为什么呢?) 因为它的示例代码写的太简单了!我真的没看太懂 ...
- OpenJudge 2721 忽略大小写比较字符串大小
1.Link: http://bailian.openjudge.cn/practice/2721/ 2.Content: 总时间限制: 1000ms 内存限制: 65536kB 描述 一般我们用st ...
- Redirect and POST in ASP.NET
http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET
- httpc服务器错误类型大全
HTTP 400 - 请求无效HTTP 401.1 - 未授权:登录失败HTTP 401.2 - 未授权:服务器配置问题导致登录失败HTTP 401.3 - ACL 禁止访问资源HTTP 401.4 ...
- stl::search
template<class ForwardIt1, class ForwardIt2> ForwardIt1 search(ForwardIt1 first, ForwardIt1 la ...
- ubuntu server版连接vpn服务器
命令行的方法: 1.要下载pptp的客户端 sudo apt-get install pptp-linux 2.创建连接 sudo pptpsetup –create vpn001 –s ...
- PHP学习之环境搭建
计算机环境 win7 64位 搭建 apache-httpd-2.2-win64 + php-5.3.6-Win32-VC9-x64 +MySQL_5.5.13_winx64开发环境 参考: ...