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
{
    int left;
    int right;
    int val;
    int dep;
} s[maxn];

int n;
int a[maxn];
int max_dep,n1,n2;

void dfs(int x,int dep)
{
    max_dep=max(max_dep,dep);
    s[x].dep=dep;
    if(s[x].left!=-1) dfs(s[x].left,dep+1);
    if(s[x].right!=-1) dfs(s[x].right,dep+1);
}

void DFS(int x)
{
    if(s[x].dep==max_dep) n1++;
    else if(s[x].dep==max_dep-1) n2++;
    if(s[x].left!=-1) DFS(s[x].left);
    if(s[x].right!=-1) DFS(s[x].right);
}

int main()
{
    scanf("%d",&n);

    if(n==0) printf("0 + 0 = 0\n");
    else
    {
        for(int i=1; i<=n; i++) scanf("%d",&a[i]);

        for(int i=0; i<=n; i++) s[i].left=s[i].right=-1;
        int id=0;
        s[id++].val=a[1];

        for(int i=2; i<=n; i++)
        {
            int now=0;
            while(1)
            {
                if(a[i]<=s[now].val)
                {
                    if(s[now].left!=-1) now=s[now].left;
                    else
                    {
                        s[now].left=id;
                        s[id++].val=a[i];
                        break;
                    }
                }

                else
                {
                    if(s[now].right!=-1) now=s[now].right;
                    else
                    {
                        s[now].right=id;
                        s[id++].val=a[i];
                        break;
                    }
                }
            }
        }

        max_dep=n1=n2=0;
        dfs(0,1);
        DFS(0);
        printf("%d + %d = %d\n",n1,n2,n1+n2);
    }
    return 0;
}
时间: 2024-10-17 16:54:02

PAT (Advanced Level) 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 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

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

PAT (Advanced Level) 1004. Counting Leaves (30)

简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<vector> using namespace std; const int maxn=100+10; vector<int>g[maxn]; int n,m; int ans[maxn]; int root; i

PAT (Advanced Level) 1049 Counting Ones

题解 now - 当前位的数值 left - 在now左边的所有数字 right - 在now右边的所有数字 mul - right的数量级,如 10,100,100 ① 如果 now == 0 ,当now可以为1时,左边的数值必须为(0 ~ (left-1)),右边的数值可以是 0 ~ 999… ,所以情况有 left*mul ② 如果 now == 1 ,在第①种情况下,再加上right+1,因为当前位已经为1,所以右边的数right在当前范围下是什么无所谓,所以情况为 left*mul+r

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

PAT (Advanced Level) 1099. Build A Binary Search Tree (30)

预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct Node { int