1110 Complete Binary Tree

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

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

        注意到题目要求就20个节点,因此可以用将节点按顺序填入一维顺序数组中,看其是否为完全二叉树,如果是则数组连续从1到n都有值,否则不是。

        其中父节点的左孩子节点位置是 : 父节点位置*2,右节点位置是父节点位置*2+1。

#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
using namespace std;
struct node{
    int left = -1,right = -1;
    int index = -1;
};
int main(){
    int n;
    cin >> n;
    vector<bool> visit(n,false);
    vector<node> v(n);
    vector<int> vv(10000,-1);
    for(int i = 0; i < n; i++){
        string s1,s2;
        cin >>s1 >> s2;
        if(s1[0]!='-')
        {
            int d = stoi(s1);
            v[i].left = d;
            visit[d] = true;
        }
        if(s2[0]!='-')
        {
            int d = stoi(s2);
            v[i].right = d;
            visit[d] = true;
        }
    }
    int root;
    for(int i = 0; i < n; i++)
        if(!visit[i])
        {
            root = i;
            break;
        }
    v[root].index = 1;
    vv[1] = root;
    queue<int> q;
    q.push(root);
    while(!q.empty()){
        int index_node = q.front();
        q.pop();
        if(v[index_node].left!=-1)
        {
            int k = v[index_node].left;
            v[k].index = v[index_node].index*2;
            q.push(k);
            vv[v[index_node].index*2] = k;
        }
        if(v[index_node].right!=-1)
        {
            int k = v[index_node].right;
            v[k].index = v[index_node].index*2+1;
            q.push(k);
            vv[v[index_node].index*2+1] = k;
        }
    }
    int count1 = 0;
    for(int i = 1; i <= n; i++)
        if(vv[i]!=-1)
            count1++;
    if(count1==n)
        cout << "YES " <<vv[n];
    else    cout << "NO "<<root;
    return 0;
}

构造一个完整的二叉搜索树(Complete Binary Search Tree, CBST)涉及两个关键方面:一是确保该树是一个完全二叉树,二是满足二叉搜索树的性质。 ### 完全二叉树的特点 完全二叉树是指除最后一层外,其余每一层都被完全填充,并且所有节点都必须尽可能靠左排列。这一特征使得我们可以有效地通过数组来表示完全二叉树,其中对于任意索引i处的节点: - 其左孩子位于位置2*i+1; - 右孩子则处于位置2*i+2; ### 构建步骤概述 给定一组数值元素如列表`[7, 4, 9, 1, 5, 8, 10]`, 我们希望将其组织成一颗CBST: #### 步骤一: 对输入数据排序 首先应对原始序列进行升序排序得到 `[1, 4, 5, 7, 8, 9, 10]` #### 步骤二: 利用分治法递归生成CBST 采用类似堆排序的方式,找到中间点作为根节点(root),左侧部分构成左子树,右侧形成右子树。 例如选取上述排好序后的中间值 `7` 设为root,则左边 `[1, 4, 5]` 成为其左子树,右边 `[8, 9, 10]` 继续按同样规则处理直至每个叶节点均创建完毕为止。 ```python class TreeNode: def __init__(self, val=0): self.val = val self.left = None self.right = None def sortedArrayToBST(nums): if not nums: return None mid = len(nums) // 2 #取整数向下找中心位置 root = TreeNode(nums[mid]) root.left = sortedArrayToBST(nums[:mid]) #递归建立左半边树 root.right = sortedArrayToBST(nums[mid+1:]) #递归建立右半边树 return root ``` 这个Python示例程序展示了如何把已排序好的数组转换成为一棵平衡CBST的基本逻辑框架。 --- 如果您还有更多疑问或需要深入探讨某些细节,请随时提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值