Find the total number of nodes in the last 2 levels of the binary tree

PTA ladder simulation contest submission link

**Two-level node statistics of binary search tree**

The binary search tree is either an empty tree or a binary tree with the following properties: if its left subtree is not empty, the values ​​of all nodes on the left subtree are less than or equal to the value of its root node ; If its right subtree is not empty, the values ​​of all nodes on the right subtree are greater than the value of its root node ; its left and right subtrees are also binary search trees . Insert a series of numbers into an initially empty binary search tree in a given order.

Your task

It is the number of nodes in the bottom 2 levels of the statistical result tree.

Input format:

Enter a positive integer N (≤1000) in the first line, which is the number of inserted digits. The second line gives N integers in the interval [−1000,1000]. The numbers are separated by spaces.

Output format:

Output the total number of nodes of the bottom 2 layers in one line.

Input sample:

9 25 30 42 16 20 20 35 -5 28

Sample output:

6

Ideas:

The construction of a binary tree is actually very simple, and the nodes that have been hung during the construction process do not need to be moved.
For each input value
1. We let it compare with the root node.
------a. If it <= the root node, find the left child of the root node, if the root node has no left child, then create a new node to serve as the left child of the root node Child; if the node has a left child, treat the left child as a root node
, repeat (1) until the value is inserted into the binary tree, and then enter the next value to insert.
- ------ If b> root node, then go to the right child of the root node, the root if no right child, then create a new node to act as the root of the right child ; If the node has a right child, then regard the right child as a root node , repeat (1) until the value is inserted into the binary tree, and then enter the next value to insert.

Figure after sample construction
Insert picture description here

The following provides an array simulation node method:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int a[1010]; 
int l[1010];//根据建二叉树的过程来存放下标,应该没啥规律吧 
int r[1010];//同理,只知道里面放的值的下标,不知道每一层的右子树都是什么值 
int cnt[1010];//cnt[i]表示深度为i的那一层有多少结点数 
int mx;//mx代表最深的深度,但是祖宗根结点那一层不算,所求的深度会少1 

void dfs(int root,int u,int deep)//0表示祖宗根结点 
{//root代表当前这颗树的根结点,u代表传入的下标值,deep表示此刻树的深	度
 // (一棵二叉树有很多树) 
	if(a[u]<=a[root])//输入的该值传给该结点的左孩子 
	{
		if(l[root]==INF)//当前根结点没有左孩子
		{
			l[root]=u;//创造一个结点来当左孩子,并把输入的值的下标放到结点里面 
			cnt[deep+1]++;//当前该树的根结点所处的深度的下一个深度多了一个结点 
			mx=max(mx,deep+1); //求最深度 
			return;//输入的该值已经放到二叉树里面 
		}
		else//当前根结点已有左孩子 
		{   //那把左孩子作为新的根结点继续向下搜索 
			dfs(l[root],u,deep+1);
			return;//执行完上一句说明输入的值已放入到二叉树中,那就可以返回,继续重新输入了 
		}
	}
	else//输入的该值传给该结点的右孩子 
	{
		if(r[root]==INF)
		{
			r[root]=u;
			cnt[deep+1]++;
			mx=max(mx,deep+1);
			return;
		}
		else
		{
			dfs(r[root],u,deep+1);
			return;
		}
	}
}
int main()
{
	int n,i;
	scanf("%d",&n);
	scanf("%d",&a[0]);
	if(n==1)
	printf("1");
	else
	{
		memset(l,INF,sizeof(l));//INF可认为相当于空 
		memset(r,INF,sizeof(r));
		for(i=1;i<n;i++)
		{
			scanf("%d",&a[i]);
			dfs(0,i,0);
		}
		printf("%d\n",cnt[mx]+cnt[mx-1]);
	}
//	printf("%d\n",mx);
	return 0;
}

Here is the real method of creating a node: I don’t know much about the
method, but I don’t know much about pointers.

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct node 
{
	int data;
	int deep;
	struct node *lchild;
	struct node *rchild;
};//BSTNode,*BiTree;
//typedef long long ll; 
typedef node  BSTNode;//node是类型 
typedef node* BiTree;//node*是指针型类型 

int mx,s;

//   * 跟node还是跟build,此刻可理解为跟node,这样返回值的类型是地址 
BSTNode* build(BiTree root,int x,int p)//都有递归操作,与数组型类似 
{//root永远代表当前这棵树的根结点,x表示要存放的值,p表示深度 
	if(root==NULL)//用来创建结点的 
	{
		mx=max(mx,p);//求最大深度 
		BSTNode *root=(BSTNode*)malloc(sizeof(BSTNode));//需要创建一个结点 
		root->data=x;
		root->deep=p;
		root->lchild=root->rchild=NULL;
		return root;
	} 
	else 
	{
		if(x<=root->data)
			root->lchild=build(root->lchild,x,p+1);
		if(x>root->data)
			root->rchild=build(root->rchild,x,p+1);
		return root;
	}
}
void trave(BiTree root) 
{
	if(root!=NULL) 
	{
		if(root->deep==mx||root->deep==mx-1)
			s++;
		trave(root->lchild);
		trave(root->rchild);
	}
}
int main() 
{
	int n;
	scanf("%d",&n);
	mx=0;
	BiTree root=NULL;
	while(n--) 
	{
		int x;
		scanf("%d",&x);
		root=build(root,x,1);
	}
	s=0;
	trave(root);
	printf("%d\n",s);
	return 0;
}

Guess you like

Origin blog.csdn.net/Helinshan/article/details/110679739