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 Node{
    int val;
    int layer;
    Node *lchild,*rchild;
    Node(int v):val(v),layer(1),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=1;
    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+1;
            q.push(pNode->lchild);
        }
        if(pNode->rchild){
            pNode->rchild->layer=pNode->layer+1;
            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("%d + %d = %d",level[maxLevel],level[maxLevel-1],level[maxLevel-1]+level[maxLevel]);
    return 0;
} 

原文地址:https://www.cnblogs.com/kkmjy/p/9588280.html

时间: 2024-10-17 16:54:09

1115 Counting Nodes in a BST的相关文章

[二叉查找树] 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 分)建立二叉搜索树,求每层结点数目

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

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 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 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 {

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甲级目录

树 备注 1004 Counting Leaves   1020 Tree Traversals   1043 Is It a Binary Search Tree  判断BST,BST的性质 1053 Path of Equal Weight   1064 Complete Binary Search Tree  完全二叉树的顺序存储,BST的性质 1066 Root of AVL Tree  构建AVL树,模板题,需理解记忆 1079 Total Sales of Supply Chain

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10