【PAT】1110. Complete Binary Tree (25)【完全二叉树】

2024-04-12 06:08

本文主要是介绍【PAT】1110. Complete Binary Tree (25)【完全二叉树】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述

Given a tree, you are supposed to tell if it is a complete binary tree.

翻译:给定一棵树,你需要说出它是否是一棵完全二叉树。

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤20) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a - will be put at the position. Any pair of children are separated by a space.

翻译:每个输入文件包含一组测试数据。对于每组测试数据,第一行包括一个正整数N(≤20) ,代表树的总结点数——假设所有节点被标记为0到N-1。接下来N行,每一行描述一个节点,给出节点左右孩子的编号。如果孩子不存在,则在该位置输出‘-‘。任何一对孩子节点之间用空格隔开。

Output Specification:

For each case, print in one line YES and the index of the last node if the tree is a complete binary tree, or NO and the index of the root if not. There must be exactly one space separating the word and the number.

翻译:对于每组输入数据,如果该树是一棵完全二叉树,输出YES和最后一个节点的编号。否则输出NO和根节点的编号。在单词和数字之间必须有一个空格。


Sample Input 1:

9
7 8
- -
- -
- -
0 1
2 3
4 5
- -
- -


Sample Output 1:

YES 8


Sample Input 2:

8
- -
4 5
0 6
- -
2 3
- 7
- -
- -


Sample Output 2:

NO 1


解题思路

首先读入数据时由于可能为’-’,所以用%s读取,然后提取出数字,不用%c的原因是可能为两位数字。然后记录每个节点的父节点,然后找出根节点。判断是否为完全二叉树的方法有很多,我的作法是将每个点重新编号,左孩子节点的编号为父节点的2倍,右孩子节点的编号为父节点的2倍+1,然后在对编号进行搜索,如果是完全二叉树则从1—N每个编号都应存在。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
int N;
int a[21][2];
int f[21],troot;
int ans[21],v[21];
void Judge(int root,int vocation,int parent){ans[root]=parent*2+vocation;int lc=a[root][0],rc=a[root][1];if(lc>=0)Judge(lc,0,ans[root]);if(rc>=0)Judge(rc,1,ans[root]);
}
int main(){scanf("%d",&N);for(int i=0;i<N;i++)f[i]=-1;char l[3],r[3];for(int i=0;i<N;i++){scanf("\n%s %s",l,r);if(l[0]=='-')a[i][0]=-1;else {int tmp=0;for(int j=0;j<strlen(l);j++){tmp=tmp*10+(l[j]-'0');	}a[i][0]=tmp;f[tmp]=i;}if(r[0]=='-')a[i][1]=-1;else {int tmp=0;for(int j=0;j<strlen(r);j++){tmp=tmp*10+(r[j]-'0');	}a[i][1]=tmp;f[tmp]=i;}}for(int i=0;i<N;i++){if(f[i]==-1)troot=i;}int temp=0;Judge(troot,1,0);int i,lastNode;for(i=0;i<N;i++){if(ans[i]>N)continue;else if(!v[ans[i]])v[ans[i]]=1;if(ans[i]==N)lastNode=i;}for(i=1;i<=N;i++){if(!v[i])break;}if(i==N+1)printf("YES %d\n",lastNode);else printf("NO %d\n",troot);return 0;
}

这篇关于【PAT】1110. Complete Binary Tree (25)【完全二叉树】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

uva 575 Skew Binary(位运算)

求第一个以(2^(k+1)-1)为进制的数。 数据不大,可以直接搞。 代码: #include <stdio.h>#include <string.h>const int maxn = 100 + 5;int main(){char num[maxn];while (scanf("%s", num) == 1){if (num[0] == '0')break;int len =

HDU 2159 二维完全背包

FATE 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务。久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级。现在的问题是,xhd升掉最后一级还需n的经验值,xhd还留有m的忍耐度,每杀一个怪xhd会得到相应的经验,并减掉相应的忍耐度。当忍耐度降到0或者0以下时,xhd就不会玩这游戏。xhd还说了他最多只杀s只怪。请问他能

zoj 1721 判断2条线段(完全)相交

给出起点,终点,与一些障碍线段。 求起点到终点的最短路。 枚举2点的距离,然后最短路。 2点可达条件:没有线段与这2点所构成的线段(完全)相交。 const double eps = 1e-8 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;

leetcode105 从前序与中序遍历序列构造二叉树

根据一棵树的前序遍历与中序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7] 返回如下的二叉树: 3/ \9 20/ \15 7   class Solution {public TreeNode buildTree(int[] pr

树(Tree)——《啊哈!算法》

树 什么是树?树是一种特殊的图,不包含回路的连通无向树。 正因为树有着“不包含回路”这个特点,所以树就被赋予了很多特性。 一棵树中的任意两个结点有且仅有唯一的一条路径连通。一棵树如果有n个结点,那么它一定恰好有n-1条边。在一棵树中加一条边将会构成一个回路。 一棵树有且只有一个根结点。 没有父结点的结点称为根结点(祖先)。没有子结点的结点称为叶结点。如果一个结点既不是根结点也不是叶

PHP实现二叉树遍历(非递归方式,栈模拟实现)

二叉树定义是这样的:一棵非空的二叉树由根结点及左、右子树这三个基本部分组成,根据节点的访问位置不同有三种遍历方式: ① NLR:前序遍历(PreorderTraversal亦称(先序遍历)) ——访问结点的操作发生在遍历其左右子树之前。 ② LNR:中序遍历(InorderTraversal) ——访问结点的操作发生在遍历其左右子树之中(间)。 ③ LRN:后序遍历(PostorderT

【JavaScript】LeetCode:21-25

文章目录 21 最大子数组和22 合并区间23 轮转数组24 除自身以外数组的乘积25 缺失的第一个正数 21 最大子数组和 贪心 / 动态规划贪心:连续和(count)< 0时,放弃当前起点的连续和,将下一个数作为新起点,这里提供使用贪心算法解决本题的代码。动态规划:dp[i]:以nums[i]为结尾的最长连续子序列(子数组)和。 dp[i] = max(dp[i - 1]

226 Invert Binary Tree

//226 Invert Binary Tree//算法思路:主要使用递归算法public class Solution {public TreeNode invertTree(TreeNode root) {//1 出口 空节点if (root==null)return null;//2 递归 调用自己TreeNode left = root.left;TreeNode right = ro

在二叉树中找到两个节点的最近公共祖先(基于Java)

如题  题解 public int lowestCommonAncestor(TreeNode root, int o1, int o2) {//记录遍历到的每个节点的父节点。Map<Integer, Integer> parent = new HashMap<>();Queue<TreeNode> queue = new LinkedList<>();parent.put(roo

数据结构--二叉树(C语言实现,超详细!!!)

文章目录 二叉树的概念代码实现二叉树的定义创建一棵树并初始化组装二叉树前序遍历中序遍历后序遍历计算树的结点个数求二叉树第K层的结点个数求二叉树高度查找X所在的结点查找指定节点在不在完整代码 二叉树的概念 二叉树(Binary Tree)是数据结构中一种非常重要的树形结构,它的特点是每个节点最多有两个子节点,通常称为左子节点和右子节点。这种结构使得二叉树在数据存储和查找等方面具