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 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 [?10001000] 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:

9
25 30 42 16 20 20 35 -5 28

Sample Output:

2 + 4 = 6思路:  通过先序遍历建立二叉树,然后使用先序遍历求每层结点数。
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<set>
#include<stack>
using namespace std;
struct Node
{
    int data;
    Node *lchild,*rchild;
};

void insertBST(Node *&root,int key)
{
    if(root==nullptr)
    {
        root=new Node;
        root->data=key;
        root->lchild=root->rchild=nullptr;
        return;
    }
    if(key>root->data)
        insertBST(root->rchild,key);
    else
        insertBST(root->lchild,key);
}

int level[1001];
int maxLevel=0;

void dfs(int depth,Node *root)
{
    if(root==nullptr)
        return;
    level[depth]++;
    maxLevel=max(depth,maxLevel);
    dfs(depth+1,root->lchild);
    dfs(depth+1,root->rchild);
}

int main()
{
    int n;
    cin>>n;
    //int a[n+1];
    Node *root=nullptr;
    for(int i=0;i<n;i++)
    {
        int temp;
        cin>>temp;
        insertBST(root,temp);
    }

    dfs(1,root);
//    for(int i=1;i<=maxLevel;i++)
//        cout<<level[i]<<" ";
//    cout<<endl;
    cout<<level[maxLevel]<<" + "<<level[maxLevel-1]<<" = "<<level[maxLevel]+level[maxLevel-1];

    return 0;
}
 

原文地址:https://www.cnblogs.com/zhanghaijie/p/10308812.html

时间: 2024-11-10 02:24:33

1115 Counting Nodes in a BST (30 分)建立二叉搜索树,求每层结点数目的相关文章

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

1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 nod

1115 Counting Nodes in a BST (30)

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

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

1115 题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int INF = 0x7FFFFFFF; const int maxn = 1e5 + 10; int n, cn

PAT (Advanced Level) 1115. Counting Nodes in a BST (30)

简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=100000+10; struct Node {

PAT 天梯赛 是否同一棵二叉搜索树&#160;&#160;&#160;(25分)(二叉搜索树)

给定一个插入序列就可以唯一确定一棵二叉搜索树.然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到.例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果.于是对于输入的各种插入序列,你需要判断它们是否能生成一样的二叉搜索树. 输入格式: 输入包含若干组测试数据.每组数据的第1行给出两个正整数NNN (≤10\le 10≤10)和LLL,分别是每个序列插入元素的个数和需要检查的序列个数.第2行给出NNN个以空格分隔的正整数,作为初始插入序列.最后LL

538. Convert BST to Greater Tree 二叉搜索树转换为更大树

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

7-31 笛卡尔树 (25分)--判断二叉搜索树,小顶堆

先初步判断是否满足二叉搜索树和小顶堆(针对每一颗最小的子树),如果都满足,进一步判断整棵树是否满足. 1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 using namespace std; 5 typedef struct node 6 { 7 int K1; 8 int K2; 9 int L; 10 int R; 11 }node_arr[1001]; 12 node_arr s;

PAT 1115 Counting Nodes in a BST

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

1115 Counting Nodes in a BST

题意:给出一棵二叉搜索树的插入序列,要求该树最后两层的结点个数. 思路:在树结点中增加一个数据域layer,表示该结点所在的层次.另外,设置数组level[]和变量maxLevel,level[i]表示第i层的结点个数,maxLevel表示树的最大层次,在层序遍历时更新即可. 代码: #include <cstdio> #include <queue> using namespace std; int level[1005]={0}; int maxLevel=0; struct