PAT 1115. Counting Nodes in a BST (30)
来源:互联网 发布:南京联迪软件 编辑:程序博客网 时间:2024/04/30 19:29
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 or equal to the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
Insert a sequence of numbers into an initially empty binary search tree. Then you are supposed to count the total number of nodes in the lowest 2 levels of the resulting tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=1000) which is the size of the input sequence. Then given in the next line are the N integers in [-1000 1000] which are supposed to be inserted into an initially empty binary search tree.
Output Specification:
For each case, print in one line the numbers of nodes in the lowest 2 levels of the resulting tree in the format:
n1 + n2 = n
where n1 is the number of nodes in the lowest level, n2 is that of the level above, and n is the sum.
Sample Input:925 30 42 16 20 20 35 -5 28Sample Output:
2 + 4 = 6
题目大意
有一棵空的BST,然后给你一个大小为N的序列,插入到BST中。求最底层和次底层的结点数。
解题思路
用数组tree[][4]模拟BST,tree[i][0]为i结点的数据,tree[i][1]为左孩子下标,tree[i][2]为右孩子下标,tree[i][3]为当前i结点的深度。
深搜过程模拟BST的插入,计算深度。
坑点:1、n=1时,应该输出1 + 0 = 1
#include <cstdio>#include <vector>#include <map>#include <set>#include <cstdlib>#include <climits>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define ll long longconst int MAXN = 1000 + 5;const int MAXM = 100000 + 5;const int INF = 0x7f7f7f7f;template <class XSD> inline XSD f_min(XSD a, XSD b) { if (a > b) a = b; return a; }template <class XSD> inline XSD f_max(XSD a, XSD b) { if (a < b) a = b; return a; }int n;int tree[MAXN][4];///0:数据 1:左子树 2:右子树 3:深度int Tdeep;void Dfs(int pos, int x, int xi, int deep){ if(tree[pos][0]>=x){///左子树 if(tree[pos][1]==-1){///为空 tree[pos][1] = xi; tree[xi][3] = deep+1; Tdeep = f_max(Tdeep, (deep+1)); } else Dfs(tree[pos][1], x, xi, deep+1);///不为空 }else{///右子树 if(tree[pos][2]==-1){///为空 tree[pos][2] = xi; tree[xi][3] = deep+1; Tdeep = f_max(Tdeep, (deep+1)); } else Dfs(tree[pos][2], x, xi, deep+1);///不为空 }}void Getdata(){ Tdeep = 0; memset(tree, -1, sizeof tree); scanf("%d", &tree[1][0]); tree[1][3] = 1; for(int i=2; i<=n; i++){ scanf("%d", &tree[i][0]); Dfs(1, tree[i][0], i, 1); }}void Solve(){ if(n==1){printf("1 + 0 = 1\n");return;} int x=0, y=0; for(int i=1; i<=n; i++){ if(tree[i][3]==Tdeep) x++; else if(tree[i][3]==Tdeep-1) y++; } printf("%d + %d = %d\n", x, y, x+y);}int main(){ while(~scanf("%d", &n)){ Getdata(); Solve(); } return 0;}
- PAT--1115. Counting Nodes in a BST
- 【PAT】1115. Counting Nodes in a BST
- PAT A 1115. Counting Nodes in a BST (30)
- solution Of Pat 1115. Counting Nodes in a BST (30)
- PAT 1115. Counting Nodes in a BST (30)
- pat 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30) PAT 甲级
- 【PAT】【Advanced Level】1115. Counting Nodes in a BST (30)
- PAT 1115. Counting Nodes in a BST (30) 建树
- PAT 甲级 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- gitbook将书籍git同步到github
- Base64.h封装
- 让 select 的 option 标签支持事件监听(如复制操作)
- 关于空值null的排序问题 mysql 和oracle
- [Android] Gallery选中变大效果的实现
- PAT 1115. Counting Nodes in a BST (30)
- ssm整理
- Matlab R2012a和vs 2010混合编程
- ssh框架初步搭载
- 关于修改 标签input file 样式问题!
- Java Date 比较
- 从UDP的”连接性”说起–告知你不为人知的UDP
- 小编教你怎样重装系统win7
- Java线程池的设计与实现