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 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> //建立二叉树和dfs
using namespace std;
int maxdeep=0, a=0, b=0;
struct node{
  int value;
  node* left=NULL;
  node* right=NULL;
  node(int v):value(v), left(NULL), right(NULL){
  }
};
node* insert(int v, node* root, int deep){
  if(!root){
    node* temp=new node(v);
    root =temp;
    maxdeep=(deep>maxdeep?deep:maxdeep);
  }else{
    if(v<=root->value)
        root->left=insert(v, root->left, deep+1);
    else
        root->right=insert(v, root->right, deep+1);
  }
  return root;
}
void preorder(node* root, int deep){
    if(root==NULL)
        return;
    if(deep==maxdeep-1)
        a++;
    else if(deep==maxdeep)
        b++;
    preorder(root->left, deep+1);
    preorder(root->right, deep+1);

}
int main(){
  int n, v;
  cin>>n;
  node* root=NULL;
  for(int i=0; i<n; i++){
    cin>>v;
    root=insert(v, root, 1);
  }
  preorder(root, 1);
  cout<<b<<" + "<<a<<" = "<<a+b<<endl;
  return 0;
}

原文地址:https://www.cnblogs.com/A-Little-Nut/p/9502031.html

时间: 2024-08-07 13:27:39

PAT 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

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 {

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

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

PAT 1004. Counting Leaves (30)

A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tre

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 1143 Lowest Common Ancestor[难][BST性质]

1143 Lowest Common Ancestor(30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recursively defined as a binary tree which has the following pro