判断一个树是完全二叉树

什么是完全二叉树

 若设二叉树的深度为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;
}

 

<think>好的,我需要向用户介绍如何判断一个二叉是否完全二叉树。首先,我得回忆完全二叉树的定义。完全二叉树是指除了最后一层外,其他层的节点都必须是满的,而且最后一层的节点都靠左排列。那怎么判断呢? 我记得常见的办法是用层序遍历(BFS),在遍历过程中检查是否存在不符合的情况。比如,如果遇到一个空节点后,后面还有非空节点,那这棵就不是完全二叉树。或者,如果某个节点只有右孩子而没有左孩子,这也违反了完全二叉树的性质。 步骤大概是这样的:首先进行层序遍历,将节点依次入队,包括空节点。然后在遍历过程中,一旦遇到空节点,就检查队列中剩余的节点是否还有非空的情况。如果有,就不是完全二叉树。另外,在遍历过程中,如果左孩子为空而右孩子存在,也应该立即返回false。 不过可能还有更优化的方法,比如不需要将空节点入队,而是在遍历时记录是否遇到了空节点。当遇到空节点后,后面再遇到任何非空节点都视为不满足条件。同时,检查每个节点的左右孩子是否存在,确保左孩子存在或者左右都不存在的情况。 比如,具体步骤可能如下: 1. 使用队列进行层序遍历。 2. 设置一个标志位(比如isEnd)来标记是否已经遇到了空节点。 3. 遍历每个节点时,如果已经标记isEnd为true,但当前节点不为空,则返回false。 4. 检查左孩子是否存在,如果不存在但右孩子存在,返回false。 5. 如果左孩子不存在或者右孩子不存在,则将isEnd设为true,后续节点必须都为空。 需要处理一些特殊情况,比如根节点为空的情况,这时候也是完全二叉树吗?根据定义,空可能被视为完全二叉树。 另外,举个例子可能更好理解。比如,一个的结构是否符合完全二叉树的条件。例如,完全二叉树的例子:每一层都填满,除了最后一层可能不满,但最后一层节点都靠左。而非完全二叉树的情况可能是最后一层节点中间有空缺,或者某个节点只有右孩子而没有左孩子。 可能还需要考虑不同的测试用例,比如: - 所有节点都有左右子节点,且层数相同。 - 最后一层有空缺但不在最左边。 - 某个节点左空但右存在。 - 只有左子的情况是否符合。 在代码实现上,可以用队列来辅助。初始化队列,加入根节点。设置一个标志位为false。然后循环处理队列中的每个节点。对于当前节点,如果标志位为true且当前节点不为空,则返回false。否则,如果当前节点不为空,将左右子节点加入队列。如果当前节点为空,设置标志位为true。不过这可能不够,因为有些情况需要检查子节点是否存在。 或者另一种方法是,在遍历过程中,当遇到第一个空节点时,之后的节点必须全为空。同时,检查每个节点的左右子节点是否满足完全二叉树的条件。例如,当左子节点不存在时,右子节点必须也不存在。 需要确保所有可能的情况都被覆盖到。例如,当左子节点存在而右子节点不存在时,后续的所有节点都必须是叶子节点,且之后的层数不能有节点。 可能还需要注意处理节点数为1的情况,或者只有左子的情况。 总结下来,判断完全二叉树的步骤大致是: 1. 层序遍历二叉。 2. 检查是否在遇到空节点后还有非空节点。 3. 检查每个节点的左右子节点是否符合完全二叉树的结构,如不存在只有右子节点的情况。 实现时,可以用一个队列来进行层序遍历,并在遍历过程中维护一个标记,表示是否已经遇到了空节点。每次处理节点时,如果标记已设置,但当前节点存在,则返回false。同时,处理每个节点的左右子节点,如果左子节点不存在而右子节点存在,返回false。否则,将存在的子节点入队,如果任何一个子节点不存在,则标记开始检查后续是否全为空。 这样应该能覆盖所有的情况,正确判断是否完全二叉树。</think>要判断一个二叉是否完全二叉树,可以按照以下步骤进行: --- ### **一、完全二叉树的定义** 完全二叉树需满足两个条件: 1. **结构完整性**:除最后一层外,其他层必须完全填满。 2. **最后一层左对齐**:最后一层的所有节点必须尽可能靠左排列,不能存在“中间空缺”。 --- ### **二、判断方法(层序遍历法)** 通过层序遍历(BFS)检查节点是否符合规则: #### **步骤 1:层序遍历二叉** 使用队列逐层遍历所有节点,包括空节点(例如将空节点用 `null` 标记)。 #### **步骤 2:标记空节点** 在遍历过程中,若遇到第一个空节点,则后续所有节点必须也为空。若后续出现非空节点,则不是完全二叉树。 #### **步骤 3:检查子节点规则** 若某个节点的左子节点为空,但右子节点非空,则违反完全二叉树的定义。 --- ### **三、具体实现逻辑** 1. **初始化队列**:将根节点入队。 2. **标记是否遇到空节点**:设置标志位 `hasNull` 初始为 `false`。 3. **循环处理队列**: - 出队当前节点。 - 若当前节点为空,设置 `hasNull = true`。 - 若当前节点非空: - 若 `hasNull` 已为 `true`,说明之前有空节点,直接返回 `false`。 - 检查左子节点是否存在:若左空右非空,返回 `false`。 - 将左右子节点入队(包括空节点)。 --- ### **四、代码示例(Python)** ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def isCompleteTree(root): if not root: return True queue = [root] hasNull = False while queue: node = queue.pop(0) if not node: hasNull = True else: if hasNull: return False # 检查左子节点是否存在或右子节点是否非法 if node.left is None and node.right is not None: return False queue.append(node.left) queue.append(node.right) return True ``` --- ### **五、实例验证** 1. **完全二叉树示例**: ``` 1 / \ 2 3 / \ / 4 5 6 ``` 层序遍历顺序为 `1,2,3,4,5,6`,满足条件。 2. **非完全二叉树示例**: ``` 1 / \ 2 3 \ \ 4 5 ``` 错误点:节点2的左子节点为空但右子节点非空。 --- ### **六、复杂度分析** - **时间复杂度**:$$O(n)$$,需遍历所有节点。 - **空间复杂度**:$$O(n)$$,队列最多存储一层节点。 通过上述步骤,可以高效判断二叉是否完全二叉树
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值