【PAT】1115. Counting Nodes in a BST (30)【树的层次遍历】

2024-04-12 06:08

本文主要是介绍【PAT】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 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.

翻译:一棵二叉搜索树(BST) 按照以下规则递归定义一棵二叉树:

  • 左子树的节点的键值小于等于该节点的键值。
  • 右子树的节点的键值大于该节点的键值。
  • 左右子树都必须也是二叉搜索树。

将一个数列插入到一棵空初始二叉搜索树中。接着你需要计算出最后得到的树的最后两层节点的个数。

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 [−1000,1000] which are supposed to be inserted into an initially empty binary search tree.

翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括一个正整数N(≤1000),表示输入队列的长度。接下来一行包括 [−1000,1000] 的N个需要被插入空二叉搜索树的整数。

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.

翻译:对于每组输入数据,按照以下格式输出一行最后得到的树的最后两层节点之和:
n1 + n2 = n
n1表示最后一层的节点数,n2表示它的上一层的节点数,n表示它们之和。


Sample Input:

9
25 30 42 16 20 20 35 -5 28


Sample Output:

2 + 4 = 6


解题思路

将节点顺序插入二叉搜索树,然后对树进行层次遍历,用bottom和abovebottom来记录最后一层和上一层的值,详见代码。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#define INF 99999999
#define bug puts("Hello\n")
using namespace std;
int N,BST[1010][3];//0存放节点值,1存放左节点编号,2存放右节点编号 
void join(int a,int root){if(BST[a][0]<=BST[root][0]){if(BST[root][1]!=0){join(a,BST[root][1]);}else BST[root][1]=a;}else{if(BST[root][2]!=0){join(a,BST[root][2]);}else BST[root][2]=a;	}
}typedef pair<int,int>P;
queue<P>q;
int level[1010],bottomlevel=0,bottom=0,abovebottom=0;
void bfs(){q.push(P(0,1));while(!q.empty()){P tmp=q.front();q.pop();level[tmp.first]=tmp.second;if(bottomlevel<tmp.second){bottomlevel=tmp.second;abovebottom=bottom;bottom=1;}else if(bottomlevel==tmp.second){bottom++;	}if(BST[tmp.first][1])q.push(P(BST[tmp.first][1],tmp.second+1));if(BST[tmp.first][2])q.push(P(BST[tmp.first][2],tmp.second+1));}
}int main(){scanf("%d",&N);for(int i=0;i<N;i++){scanf("%d",&BST[i][0]);if(i!=0)join(i,0);} bfs();int ans=bottom+abovebottom;printf("%d + %d = %d\n",bottom,abovebottom,ans);return 0;
}

这篇关于【PAT】1115. Counting Nodes in a BST (30)【树的层次遍历】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/896315

相关文章

Python应用开发——30天学习Streamlit Python包进行APP的构建(9)

st.area_chart 显示区域图。 这是围绕 st.altair_chart 的语法糖。主要区别在于该命令使用数据自身的列和指数来计算图表的 Altair 规格。因此,在许多 "只需绘制此图 "的情况下,该命令更易于使用,但可定制性较差。 如果 st.area_chart 无法正确猜测数据规格,请尝试使用 st.altair_chart 指定所需的图表。 Function signa

PAT-1039 到底买不买(20)(字符串的使用)

题目描述 小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。例如,YrR8RrY是小红想做的珠串;那么ppRYYGrrYBR2258可以

PAT-1028

题目描述 某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人,而今天是2014年9月6日,所以超过200岁的生日和未出生的生日都是不合理的,应该被过滤掉。 输入描述: 输入在第一行给出正整数N,取值在(0, 105];随后N行,每行给出1个人的姓名(由不超过5个

二叉树三种遍历方式及其实现

一、基本概念 每个结点最多有两棵子树,左子树和右子树,次序不可以颠倒。 性质: 1、非空二叉树的第n层上至多有2^(n-1)个元素。 2、深度为h的二叉树至多有2^h-1个结点。 3、对任何一棵二叉树T,如果其终端结点数(即叶子结点数)为n0,度为2的结点数为n2,则n0 = n2 + 1。 满二叉树:所有终端都在同一层次,且非终端结点的度数为2。 在满二叉树中若其深度为h,则其所包含

LeetCode 算法:二叉树的中序遍历 c++

原题链接🔗:二叉树的中序遍历 难度:简单⭐️ 题目 给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。 示例 1: 输入:root = [1,null,2,3] 输出:[1,3,2] 示例 2: 输入:root = [] 输出:[] 示例 3: 输入:root = [1] 输出:[1] 提示: 树中节点数目在范围 [0, 100] 内 -100 <= Node.

2024年6月24日-6月30日(ue独立游戏为核心)

试过重点放在独立游戏上,有个indienova独立游戏团队是全职的,由于他们干了几个月,节奏暂时跟不上,紧张焦虑了。五一时也有点自暴自弃了,实在没必要,按照自己的节奏走即可。精力和时间也有限,放在周末进行即可。除非哪天失业了,再也找不到工作了,再把重心放在独立游戏上。 另外,找到一个同样业余的美术,从头做肉鸽游戏,两周一次正式交流即可。节奏一定要放慢,不能影响正常工作生活。如果影响到了,还不如自

韩顺平0基础学java——第30天

p600-611 坦克大战! 艰难推进中 坦克大战-子弹 发射子弹 1.当发射一颗子弹后,就相当于启动一个线程 2.玩家拥有子弹对象,当按下J时,就启动发射行为(线程),让子弹不停移动,形成射击的过程。 3.面板mypanel需要不停重绘,才能出现这个效果 4.当子弹移动到面板边界时,就销毁子弹线程。   增加功能:让敌人发射子弹,且可以有多颗子弹。 1.在敌人坦克类中增加V

leetcode刷题(97)——106. 从中序与后序遍历序列构造二叉树

根据一棵树的中序遍历与后序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 中序遍历 inorder = [9,3,15,20,7]后序遍历 postorder = [9,15,7,20,3] 返回如下的二叉树: 3/ \9 20/ \15 7 看下后序和中序遍历的框架: void traverse(TreeNode root) {trave

leetcode刷题(97)——105. 从前序与中序遍历序列构造二叉树

根据一棵树的前序遍历与中序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7] 返回如下的二叉树: 3/ \9 20/ \15 7 1.先回顾前序遍历和中序遍历的框架: void traverse(TreeNode root) {//

刷题——二叉树的前序遍历

二叉树的前序遍历_牛客题霸_牛客网 双指针法: /*** struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* };*/class Solution {public:/*