判断一个树是完全二叉树

什么是完全二叉树

 若设二叉树的深度为k,除第 k 层外,其它各层 (1~k-1) 的结点数都达到最大个数,第k 层所有的结点都连续集中在最左边,这就是完全二叉树。

若采用连续储存的方式存放二叉树,则节点下标之间的关系:

若起始索引为1

若某个节点的下标为 i ,则这个节点的父节点的下标为 i / 2。

若某个节点下标为 i ,且节点的度为2,则这个节点的左子节点的下标为 2 * i ,右子节点的下标为 2 * i +1 。

按数组索引从小到大存的是完全二叉树的层序遍历序列

如何判断一棵二叉树是完全二叉树

对于节点值为正数的二叉树

初始化数组所有元素的值为-1

从整棵树的根节点开始,先序遍历整棵树,将节点按照索引保存在数组里,并且记录最后一个节点的索引,记为end。遍历结束后,扫描数组(从1到end),若发现某个数组元素值为-1,则不是完全二叉树,反之则为完全二叉树

例题

1064 甲级Complete Binary Search Tree (30分)

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568

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 the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input:

10
1 2 3 4 5 6 7 8 9 0

Sample Output:

6 3 8 1 5 7 9 0 2 4

 题目大意:

给定一颗完全二叉排序树的每个节点的值,要求输出该树的层序遍历序列

分析:

二叉排序树的中序遍历序列是按照元素的值从小到大排序的,并且该二叉树还是完全二叉树,所以若用数组存储该二叉树,那么该数组保存的元素就是该树的层序遍历序列。因此只要我们按照中序遍历将树还原出来并保存在数组中,就可以得到答案

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int n, index = 0;
vector<int> vec(10000), ans(1000);

void inorder(int t){
    if(t >= n)
        return;
    inorder(2 * t + 1);
    ans[t] = vec[index++];    // 中序遍历,值与下标对号入座
    inorder(2 * t + 2);
}

int main(){

    scanf("%d\n", &n);
    for(int i=0; i<n; i++){
        scanf("%d", &vec[i]);
    }
    sort(vec.begin(), vec.begin()+n);
    inorder(0);
    for(int i=0; i<n; i++){
        if(i)
            printf(" ");
        printf("%d", ans[i]);
    }
    return 0;
}

PTA甲级  1110 Complete Binary Tree (25分)

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232

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.

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.

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

题目大意:

给定一个二叉树,若这棵二叉树是完全二叉树则输出YES并输出这颗二叉树中的最后一个节点的序号。若这棵二叉树不是完全二叉树,则输出NO并输出根节点的编号值

分析:

因为节点的编号为非负数,所以可以初始化记录树的数组所有元素为-1

还需要找到根节点的编号,入度为0的点就是根节点

按照给定的数据建树,然后从根节点先序遍历整棵树,将节点的值保存在数组里,并记录最后一个节点的下标end。之后从1到end遍历数组,若存在-1则可判定该二叉树不是完全二叉树,反之则为完全二叉树。

#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
    int l,r;
} node;
node t[110];
int a[110];
int fun(string s)
{
    if(s=="-")
        return -1;
    int num=0;
    for(int i = 0; i<s.length(); i++)
    {
        num=num*10+s[i]-'0';
    }
    return num;
}
int maxId;
void dfs(int root,int index)
{
    if(root!=-1)
    {
        if(maxId<index)
        {
            maxId=index;
        }
        a[index]=root;
        dfs(t[root].l,index*2);
        dfs(t[root].r,index*2+1);
    }
}
int vis[110];
int main()
{
    int n,i,maxn=0;
    string s1,s2;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>s1>>s2;
        t[i].l=fun(s1);
        t[i].r=fun(s2);
        vis[t[i].l]++,vis[t[i].r]++;
    }
    int root=-1,num=0;
    for(i=0; i<n; i++)
    {
        if(vis[i]==0)
        {
            if(root==-1)
                root=i;
            num++;
        }
    }
    memset(a,-1,sizeof(a));
    dfs(root,1);
    for(i=1;i<=maxId;i++)
    {
        if(a[i]==-1)
        {
            break;
        }
    }
    if(i==maxId+1)
    {
        cout<<"YES"<<" "<<a[maxId];
    }
    else
    {
        cout<<"NO"<<" "<<root<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值