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

思路:在树结点中增加一个数据域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. python关于sys.argv列表的应用场景

    python关于sys.argv列表的应用场景 1.需求很简单: 使用 sys.argv 获取命令行参数(即要判断的文件名),然后判断文件是否存在,文件存在再进一步判断是否有读权限 2.分析: (1) ...

  2. lightoj1197区间素数筛

    模板题,不过好像有点问题,当a==1的时候,答案把一也算进去了,要减去 #include<map> #include<set> #include<cmath> #i ...

  3. 五十 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现我的搜索以及热门搜索

    第三百七十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现我的搜索以及热门 我的搜素简单实现原理我们可以用js来实现,首先用js获取到 ...

  4. python tesseract-ocr 基础验证码识别功能(Windows)

    一.环境 windows 7 x64 Python 3 + 二.安装 1.tesseract-ocr安装 http://digi.bib.uni-mannheim.de/tesseract/ 2.py ...

  5. PhantomJS 和Selenium模拟页面js点击

    由于自己不怎么会javascripts,无法找全所有的参数进行模拟提交,所以只能寻求Selenium和PhantpmJS的方式. 先说下ubuntu上怎么安装相应的环境,尤其PhantomJS安装比较 ...

  6. windows平台下安装Beautiful Soup

    1.Beautiful Soup 4.3.2 下载 2.下载完成后解压,放在Python的安装目录下,假设放到C:/Python 3.运行cmd,切换到C:/Python/Beautiful Soup ...

  7. 软工作业-Wc

    Wc.exe wc.exe是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数.单词数和行 ...

  8. Python基础学习(第5天)

    第3课  模块 1.模块(module) Python中一个.py文件就是一个模块,可以调用其它文件中的程序. 例:first.py def laugh(): print '哈哈哈哈哈' second ...

  9. linux使用virtualenv构建虚拟环境,requirement.txt记录包版本

    virtualenv介绍: virtualenv把是一个把python应用隔离在一个虚拟环境中的工具.网上的例子较多,这里重点讲述怎么使用virtualenv来激活一个虚拟环境,并且记录虚拟环境中所依 ...

  10. 在 Ubuntu 上搭建 Hadoop 分布式集群 Eclipse 开发环境

    一直在忙Android FrameWork,终于闲了一点,利用空余时间研究了一下Hadoop,并且在自己和同事的电脑上搭建了分布式集群,现在更新一下blog,分享自己的成果. 一 .环境 1.操作系统 ...