Source:

PAT A1115 Counting Nodes in a BST (30 分)

Description:

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 (≤) which is the size of the input sequence. Then given in the next line are the N integers in [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:

  1. 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:

  1. 9
  2. 25 30 42 16 20 20 35 -5 28

Sample Output:

  1. 2 + 4 = 6

Keys:

Attention:

  • BST定义有时候会不一样,等号跟左子树还是右子树要看清楚,注意审题

Code:

  1. /*
  2. Data: 2019-06-26 15:55:13
  3. Problem: PAT_A1115#Counting Nodes in a BST
  4. AC: 17:15
  5.  
  6. 题目大意:
  7. BST定义:lchild <= root < rchild
  8. 根据插入序列建立BST树,统计最底层和次底层的结点数量
  9.  
  10. 基本思路:
  11. 建树,记录结点层次,全局变量记录树的最大深度
  12. 更新最大深度的同时,统计底层和次底层的结点个数
  13. */
  14. #include<cstdio>
  15. int deep=,n1=,n2=;
  16. struct node
  17. {
  18. int data;
  19. node *lchild,*rchild;
  20. };
  21.  
  22. void Insert(node *&root, int x, int height)
  23. {
  24. if(root == NULL)
  25. {
  26. if(deep == height)
  27. n1++;
  28. else if(deep == height+)
  29. n2++;
  30. else if(deep == height-)
  31. {
  32. deep++;
  33. n2 = n1;
  34. n1 = ;
  35. }
  36. root = new node;
  37. root->data = x;
  38. root->lchild = root->rchild = NULL;
  39. }
  40. else if(x <= root->data)
  41. Insert(root->lchild, x, height+);
  42. else
  43. Insert(root->rchild, x, height+);
  44. }
  45.  
  46. int main()
  47. {
  48. #ifdef ONLINE_JUDGE
  49. #else
  50. freopen("Test.txt", "r", stdin);
  51. #endif // ONLINE_JUDGE
  52.  
  53. int n,x;
  54. scanf("%d", &n);
  55. node *root = NULL;
  56. for(int i=; i<n; i++)
  57. {
  58. scanf("%d", &x);
  59. Insert(root,x,);
  60. }
  61. printf("%d + %d = %d", n1,n2,n1+n2);
  62.  
  63. return ;
  64. }

PAT_A1115#Counting Nodes in a BST的更多相关文章

  1. PAT1115:Counting Nodes in a BST

    1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  2. PAT甲1115 Counting Nodes in a BST【dfs】

    1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tr ...

  3. 1115 Counting Nodes in a BST (30 分)

    1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tr ...

  4. [二叉查找树] 1115. Counting Nodes in a BST (30)

    1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  5. PAT 1115 Counting Nodes in a BST[构建BST]

    1115 Counting Nodes in a BST(30 分) A Binary Search Tree (BST) is recursively defined as a binary tre ...

  6. A1115. Counting Nodes in a BST

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  7. PAT A1115 Counting Nodes in a BST (30 分)——二叉搜索树,层序遍历或者dfs

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  8. PAT 甲级 1115 Counting Nodes in a BST

    https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...

  9. 1115. Counting Nodes in a BST (30)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

随机推荐

  1. N天学习一个linux命令之ssh-keygen

    用途 生成ssh加密算法需要使用到的秘钥以及管理和转换 用法 ssh-keygen [-q] [-b bits] -t type [-N new_passphrase] [-C comment] [- ...

  2. Mac OSX:最简单的安装ant方法

    安装ant最简单的方法就是通过brew.步骤如下:1. 安装brew(如果已经安装可以跳过这步).ruby -e "$(curl -fsSL https://raw.github.com/m ...

  3. Win8.1下COCOS2D-X 3.4环境搭建

     Cocos2dx_3.4开发环境搭建,并编译成APK 第一步:须要下载的:(windows64位系统下环境搭建) Ant   apache-ant-1.9.4-bin.zip NDK   and ...

  4. 【POJ 2983】Is the Information Reliable?(差分约束系统)

    id=2983">[POJ 2983]Is the Information Reliable? (差分约束系统) Is the Information Reliable? Time L ...

  5. SerializeUtil 序列化,反序列化工具类

    package cloud.app.prod.home.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutp ...

  6. SpringMVC中的 --- 异常处理

    系统异常处理器SimpleMappingExceptionResolver 处理器方法执行过程中,可能会发生异常,不想看到错误黄页,想看到一个友好的错误提示页. 自定义异常处理器 使用异常处理注解

  7. luogu1273 有限电视网

    题目大意 有一棵有根树,每个结点有一个收益,每条边有一个花费.如果要选择一个叶子结点,则根节点到该叶子结点的路径上的所有结点都必须被选择.求当总收益大于等于总花费的情况下,最多能选择多少个叶子结点. ...

  8. [RK3288][Android6.0] U-boot 启动流程小结【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/52536093 Platform: RK3288OS: Android 6.0Version: ...

  9. poj 1061(扩展欧几里得定理求不定方程)

    两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特 ...

  10. [NOIP 2014] 生活大爆炸版石头剪刀布

    [题目链接] http://uoj.ac/problem/15 [算法] 按题意模拟即可[代码] #include<bits/stdc++.h> using namespace std; ...