题意:给出一棵二叉搜索树的插入序列,要求该树最后两层的结点个数。

思路:在树结点中增加一个数据域layer,表示该结点所在的层次。另外,设置数组level[]和变量maxLevel,level[i]表示第i层的结点个数,maxLevel表示树的最大层次,在层序遍历时更新即可。

代码:

  1. #include <cstdio>
  2. #include <queue>
  3. using namespace std;
  4. ]={};
  5. ;
  6.  
  7. struct Node{
  8. int val;
  9. int layer;
  10. Node *lchild,*rchild;
  11. Node(),lchild(NULL),rchild(NULL){}
  12. };
  13.  
  14. void insert(Node* &root,int val)
  15. {
  16. if(root==NULL) {
  17. root=new Node(val);
  18. return;
  19. }
  20. if(val<=root->val) insert(root->lchild,val);//根据题意,这里是<=,要仔细!
  21. else insert(root->rchild,val);
  22. }
  23.  
  24. void levelOrderTraversal(Node* root)
  25. {
  26. queue<Node*> q;
  27. root->layer=;
  28. q.push(root);
  29. while(!q.empty()){
  30. Node* pNode=q.front();
  31. q.pop();
  32. level[pNode->layer]++;
  33. if(pNode->layer > maxLevel) maxLevel=pNode->layer;
  34. if(pNode->lchild){
  35. pNode->lchild->layer=pNode->layer+;
  36. q.push(pNode->lchild);
  37. }
  38. if(pNode->rchild){
  39. pNode->rchild->layer=pNode->layer+;
  40. q.push(pNode->rchild);
  41. }
  42. }
  43. }
  44.  
  45. int main()
  46. {
  47. int n,v;
  48. scanf("%d",&n);
  49. Node* root=NULL;
  50. while(n--){
  51. scanf("%d",&v);
  52. insert(root,v);
  53. }
  54. levelOrderTraversal(root);
  55. printf(],level[maxLevel-]+level[maxLevel]);
  56. ;
  57. }

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

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

  2. 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 ...

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

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

  4. 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 ...

  5. PAT 甲级 1115 Counting Nodes in a BST

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

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

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

  7. PAT 1115 Counting Nodes in a BST

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

  8. PAT Advanced 1115 Counting Nodes in a BST (30) [⼆叉树的遍历,BFS,DFS]

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

  9. PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)

    题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...

  10. PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】

    题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...

随机推荐

  1. server_2003_r2_standard_sp2_vl_X13-46532

    1. 安装的是 cn_win_srv_2003_r2_standard_with_sp2_vl_cd1_X13-46532.iso CD2 它没有要求装 也就没装,貌似 网上搜到 安装CD2需要另外的 ...

  2. scala学习手记25 - Curry化

    curry翻译为中文就是咖喱.意为使用curry可以让代码更有味道. scala里的curry化可以把函数从接收多个参数转换成接收多个参数列表.也就是说我们要编写的函数不是只有一个参数列表,这个参数列 ...

  3. scala学习手记18 - Any和Nothing

    Any 前面已经有两次提到过:在scala中,Any类是所有类的超类. Any有两个子类:AnyVal和AnyRef.对应Java直接类型的scala封装类,如Int.Double等,AnyVal是它 ...

  4. python脚本1_给一个半径求圆的面积和周长

    #给一个半径,求圆的面积和周长,圆周率3.14 r = int(input('r=')) print('area='+str(3.14*r*r)) print('circumference='+str ...

  5. RabbitMQ Consumer获取消息的两种方式(poll,subscribe)解析

    以下转自:http://blog.csdn.net/yangbutao/article/details/10395599 rabbitMQ中consumer通过建立到queue的连接,创建channe ...

  6. 51nod 1161 组合数,规律

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1161 显然,题目可以转化为矩阵求解,但复杂度显然时空都不允许,我们如果自 ...

  7. IOS-真机相关

    真机调试 Certificates  证书 Identifiers  标示符 Profiles  描述文件 一. 证书,安装在电脑上,只有安装了证书的电脑,才有可能进行真机调试. - All - De ...

  8. ASP.NET MVC性能优化(实际项目中)

    前言 在开发中为了紧赶项目进度而未去关注性能的问题,在项目逐渐稳定下来后发现性能令人感到有点忧伤,于是开始去关注这方面,本篇为记录在开发中遇到的问题并解决,不喜勿喷.注意:以下问题都是在移动端上出现, ...

  9. Java基础学习-extends继承(成员变量,局部变量,成员方法)

    package extend; /*面向对象-继承: * 多个类的共同成员变量和成员方法.抽取到另一个类中(父类),我们多个类就可以访问到父类的成员了 * */ class Game{ String ...

  10. mysql的5.6版本支持分区吗?

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/72291698 本文出自[我是干勾鱼的博客] 我们知道,查看mysql是否支持分区 ...