1110. Complete Binary Tree (25)

  1. 题目见这里

  2. 完全二叉树的判定而已,采用层次遍历,访问到某个结点时查探它的左右孩子情况;另外就是注意数据读入,有可能为两位(至多两位)

  3. 代码如下:
#include <stdio.h>
#define N 21

typedef struct{
    int lChild,rChild;
}BiTree;

int n,root;
BiTree bt[N];

void Read(){
    int flag[N]={0};
    int i,tmp;
    char child;
    scanf("%d",&n);
    getchar();
    for(i=0;i<n;i++){
        scanf("%c",&child);     
        if(child=='-'){
            bt[i].lChild = -1;
            getchar(); //处理空格 
        }
        else{
            tmp = child-'0';
            scanf("%c",&child);
            if(child!=' '){
                tmp = tmp*10+child-'0';
                getchar(); //处理空格 
            }
            bt[i].lChild = tmp;
            flag[tmp] = 1;
        }
        scanf("%c",&child);
        if(child=='-'){
            bt[i].rChild = -1;
            getchar(); //处理换行 
        }
        else{
            tmp = child-'0';
            scanf("%c",&child);
            if(child!='\n'){
                tmp = tmp*10+child-'0';
                getchar(); //处理换行 
            }
            bt[i].rChild = tmp;
            flag[tmp] = 1;
        }
    }
    for(i=0;i<n;i++)
        if(!flag[i]){
            root = i;
            break;
        }
}

int Judge(){
    int q[N],tmp;
    int front,rear;
    front = rear = 0;
    q[rear++] = root;
    while(front<rear){
        tmp = q[front++];
        if(bt[tmp].lChild==-1){
            //只有右孩子或者没有孩子且仍有结点未进队 
            if(bt[tmp].rChild!=-1 || rear!=n) tmp = -1;
            else{
                tmp = q[rear-1]; //最后一个结点 
            }
            break;
        }
        else{
            q[rear++] = bt[tmp].lChild;
            if(bt[tmp].rChild==-1){
                if(rear!=n) tmp = -1;
                else tmp = q[rear-1]; //最后一个结点(少写了一个else,QAQ)
                break;
            }
            else{
                q[rear++] = bt[tmp].rChild; 
            }
        }
    }
    return tmp;
}

int main(){
//  freopen("Data.txt","r",stdin);
    Read();
    int node = Judge();
    //Judge在不为完全二叉树时将node赋值为root,然后用"node==root"或者"node=root && root!=n-1"会出现下面几个问题
    //1)root刚好是最后一个结点(n=1)(node==root)
    //2)root不为最后一个结点,但root刚好为n-1(node=root && root!=n-1) 
    if(node==-1) printf("%s %d","NO",root);
    else printf("%s %d","YES",node);
    printf("\n");
    return 0;
}
To convert the given array to a complete BST, we need to perform the following steps: 1. Sort the array in ascending order 2. Construct a complete binary tree using the sorted array 3. Perform inorder traversal of the binary tree and store the elements in the original array in the same order as the traversal Here's the implementation of the to_bst(lst) function in Python: ```python def to_bst(lst): # Sort the input list lst.sort() # Construct a complete binary tree using the sorted list n = len(lst) if n == 0: return lst root = lst[n // 2] left_subtree = to_bst(lst[:n // 2]) right_subtree = to_bst(lst[n // 2 + 1:]) binary_tree = [root] + left_subtree + right_subtree # Perform inorder traversal of the binary tree and store the elements in the original array inorder_traversal(binary_tree, lst, 0) return lst def inorder_traversal(binary_tree, lst, i): # Perform inorder traversal of the binary tree and store the elements in the original array n = len(binary_tree) if i >= n: return inorder_traversal(binary_tree, lst, 2 * i + 1) lst[i] = binary_tree[i] inorder_traversal(binary_tree, lst, 2 * i + 2) ``` The to_bst(lst) function takes in the input list and returns the same list after converting it to a complete BST. The function first sorts the input list in ascending order. It then constructs a complete binary tree using the sorted list by recursively dividing the list into two halves and setting the middle element as the root of the binary tree. Finally, the function performs an inorder traversal of the binary tree and stores the elements in the original list in the same order as the traversal.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值