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, cnt[maxn], a[maxn];
int ch[maxn][2], root;  

void insert(int &x,int y)
{
    if(!x){x=y;return;}
    if(a[y]<=a[x])insert(ch[x][0],y);
    else insert(ch[x][1],y);
}

void dfs(int x,int dep)
{
    if(!x)return;
    cnt[dep]++;
    dfs(ch[x][0],dep+1);
    dfs(ch[x][1],dep+1);
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        insert(root,i);
    }
    dfs(root,1);
    for(int i=n;i;i--)
    {
        if(cnt[i])
        {
            printf("%d + %d = %d\n",cnt[i],cnt[i-1],cnt[i]+cnt[i-1]);
            break;
        }
    }
    return 0;
}
时间: 2024-08-06 03:24:04

PAT A 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

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 {

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

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 subtre

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

Two nodes of a BST are swapped, correct the BST

Two nodes of a BST are swapped, correct the BST[转载] Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST. Input Tree: 10 / 5 8 / 2 20 In the above tree, nodes 20 and 8 must be swapped to fix the tree. Following is the

PAT Advanced 1099 Build A Binary Search Tree (30) [?叉查找树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 the node's key. The right subtree of a node contains only nodes with keys gre

PAT 甲级 1004 Counting Leaves

https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Specification: Each input file contains one t