博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789220.html特别不喜欢那些随便转载别人的原创文章又不给出链接的所以不准偷偷复制博主的博客噢~~ 题意:给出一个序列,问你是否是一棵二叉搜索树或者是其镜像的前序遍历.并且输出其后序遍历. 一开始没考虑到只有一个子树...比如BST78 11 9 8 10 13 12MIRROR78 11 13 12 9 10 8所以要先判断是否为BST,不是的话再判断是…
1043 Is It a Binary Search Tree (25 分)   A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a…
简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; struct Node { int left; int right; int val; } s[maxn]; int n; int a[maxn]; int ans[maxn],tot; int h[maxn]; int k; void dfs(int x) { ans[k++]=s[x].val; ) dfs(…
模拟先说一下例子,最后为方便起见,在目的地安增加一个费用为0的加油站0 1 2 3 4 5 6 7 87.1 7.0 7.2 6.85 7.5 7.0 7.3 6.0 00 150 200 300 400 600 1000 1250 1300车子加满了最多开50*12=600的距离0.第0个加油站600以内中,找有没有比7.1小的找到7.0,那么只需要加跑150距离的汽油即可费用(150/12)*7.1=88.75到第1个加油站 1.同样往后600距离内找比7.0小的找到6.85,那么只需要加跑…
题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两层的节点个数,也可以直接一遍dfs,顺便存储各个层的节点数. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #define LEFT 1 #defin…
题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. trick: for(auto it:post) cout<<it<<((it!=post[n-1])?" ":""); 这样输出会使第0,1数据点格式错误...原因未知 cout<<post[0]; for(int i=1;i<…
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The lef subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys grea…
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys gre…
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1043 and the source code is as followed. #include<iostream> #include<vector> #include<cstdio> using namespace std; int pos; struct cell { cell *lchild…
①中文题目 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜索树: 在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL. ②思路 这个很好想的, 1.判断当前结点是否为空,如果是的,那就返回null. 2.再看当前结点的值是否跟val相等,如果等,就返回root. 3.如果当前结点的值是否<val,如果是小于,就说明输出的val在当前根的左边,就…