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

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

代码:

#include <cstdio>
#include <queue>
using namespace std;
]={};
;

struct Node{
    int val;
    int layer;
    Node *lchild,*rchild;
    Node(),lchild(NULL),rchild(NULL){}
};

void insert(Node* &root,int val)
{
    if(root==NULL) {
        root=new Node(val);
        return;
    }
    if(val<=root->val) insert(root->lchild,val);//根据题意,这里是<=,要仔细!
    else insert(root->rchild,val);
}

void levelOrderTraversal(Node* root)
{
    queue<Node*> q;
    root->layer=;
    q.push(root);
    while(!q.empty()){
        Node* pNode=q.front();
        q.pop();
        level[pNode->layer]++;
        if(pNode->layer > maxLevel) maxLevel=pNode->layer;
        if(pNode->lchild){
            pNode->lchild->layer=pNode->layer+;
            q.push(pNode->lchild);
        }
        if(pNode->rchild){
            pNode->rchild->layer=pNode->layer+;
            q.push(pNode->rchild);
        }
    }
}

int main()
{
    int n,v;
    scanf("%d",&n);
    Node* root=NULL;
    while(n--){
        scanf("%d",&v);
        insert(root,v);
    }
    levelOrderTraversal(root);
    printf(],level[maxLevel-]+level[maxLevel]);
    ;
} 

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. LeetCode第[33]题(Java):Search in Rotated Sorted Array

    题目:在翻转有序中搜索 难度:Medium 题目内容: Suppose an array sorted in ascending order is rotated at some pivot unkn ...

  2. HTML5 ——web audio API 音乐可视化(一)

    使用Web Audio API可以对音频进行分析和操作,最终实现一个音频可视化程序. 最终效果请戳这里; 完整版代码请戳这里,如果还看得过眼,请给一个start⭐ 一.API AudioContext ...

  3. spring3:对JDBC的支持 之 JDBC模板类

    7.2  JDBC模板类 7.2.1  概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDB ...

  4. 案例学习:留言本制作V1、V2

    需求V1: 请设计一个留言本功能的程序,包括普通用户发布和查看留言,管理员登录.查看和删除留言. 实验指导内容详见留言本制作(1) 需求V2: 1.请修改留言本程序的功能,添加普通用户添加登录功能,用 ...

  5. appium自动化测试(二)

    一. 获取应用包名和入口activity 获取应用包名和入口activity:aapt命令 aapt目录: 安卓sdk的build-tools目录下(如果要在cmd里直接运行,要配置环境变量,否则需要 ...

  6. 原生javascript-图片弹窗交互效果

    本人的第一个原生js插件 - picLightBox 在线例子:http://lgy.1zwq.com/photoBox/ [一]用var 声明多个变量,比每个变量都用var快多了 var sScro ...

  7. 【spark】示例:二次排序

    我们有这样一个文件          首先我们的思路是把输入文件数据转化成键值对的形式进行比较不就好了嘛! 但是你要明白这一点,我们平时所使用的键值对是不具有比较意义的,也就说他们没法拿来直接比较. ...

  8. 原创:Angular新手容易碰到的坑,随时更新,欢迎订阅

    在Angular群里回答新手问题一段时间了,有一些Angular方面的坑留在这里备查,希望能对各位有所帮助.这个文章将来会随时更新,不会单独开新章,欢迎各位订阅. Q1. <div ng-inc ...

  9. Agilent RF fundamentals (11)-Vector modulator

     Vector modulator 矢量调制器:调整信号的幅度和相位 http://www.21ic.com/app/test/201805/762401.htm

  10. [JS学习笔记]Event对象

    写在前面 学习和总结JS时会伴随性的生成一些dome,其中包含一些动态输出的结果和标注. 之前通过鸡贼的办法实现了在博客中执行JS,但很多时候需要一张干净的页面编写dome,所以尝试通过一些在线的JS ...