PAT甲级1110-complete binary tree

思路:

刚开始没有看懂题,n个节点,分别是0~n-1 !!!!
之后顺序给出对应节点的左右子树。
判断是否为完全二叉树(数据结构课本内容!)。

考虑:

用什么来存储二叉树:一维数组
完全二叉树性质:

	1.     顺序存储,从头到尾之间没有空位置!!!
	即最大下标即对应最大节点值(n-1)
	//因为是0~n-1, 所以最大下标数=n-1, 即是完全二叉树。
	
	3.   完全二叉树的孩子的下标:n*2, n*2+1.
处理——建树——dfs深搜:递归对下标操作。

个人实现:
建树的递归实现:

  1.  参数:root——当前节点值, index——当前节点下标
    
  2.   全局变量:maxi记录下标,
     	     	ans记录最后节点值。
    
  3. 思路不清楚的地方:

     			dfs实现过程,如何实现从头到尾的深搜,
     			1. 一个for循环,还是在main中:
     			main中的话,如果不是完全二叉树,下标应该会变吧。
     			2. 如果dfs中,(一般模板的实现),该怎么样写代码实现。
    

//思路代码:

#include<bits/stdc++.h>
#define MAXN 25
using namespace std;


int maxi, ans;
int a[25][3], n;

void dfs(int root, int index){
    if(index > maxi){
        maxi = index;
        ans = root;
    }
    if(index > n){
        return;
    }
    dfs(a[index][0], index*2);
    dfs(a[index][1], index*2+1);
}

int main(){
    cin >> n;
    char ch1, ch2;
    for(int i = 1; i <= n; i++){
        a[i] = i-1;
        cin >> ch1 >> ch2;
        if(ch1 == '-') a[i*2] = -1;
        else a[i*2] = stoi(ch1);
        if(ch2 == '-') a[i*2+1] = -1;
        else a[i*2+1] = stoi(ch2);
    }

        dfs(i-1, i);            //dfs 从头到尾遍历一遍???——实现

}

改正思路:
  1. 将存储节点及其子节点的存储过程 和 利用已存信息进行深搜(利用完全二叉树的性质进行处理)的过程区分开来。
  2. 利用完全二叉树特性:当下标值大于节点总数n时,说明不是完全二叉树。
  3. 深搜:从根节点开始遍历,参数root, index为每次更新的数据。

//修改后代码:

//3-段错误,答案错误:三个测试点
#include<bits/stdc++.h>
#include<cstring>
#define MAXN 105
using namespace std;


int maxi = -1, ans;
int a[MAXN][5]; //记录0-n-1 个节点是不是根,左右孩子是谁

void dfs(int root, int index){  //root为节点,index为当前节点下标值
                                //深搜的契合性——由根依次深入,直到判断到最远节点
                                //——index是统计的一个标志,树是由当前节点决定子树是谁
    if(index > maxi){
        maxi = index;
        ans = root;
    }

    if(a[root][1]!=-1) dfs(a[root][1], index*2);  //即从树的当前节点root往下遍历
    if(a[root][2]!=-1) dfs(a[root][2], index*2+1);
}

int main(){
    int n;
    cin >> n;
    char ch1, ch2;
    for(int i = 0; i <= n; i++) a[i][0] = -1;
    for(int i = 0; i < n; i++){ //0-n-1的节点

        //a[i][0]用来判断标记是否为根节点——只有一个!!!, a[i][1]左孩子值, a[i][2]右孩子值。i代表下标值。
        cin >> ch1 >> ch2;
        if(ch1 == '-'){a[i][1] = -1;}
        else { a[i][1] = ch1 - '0'; a[ch1-'0'][0] = 1;}

        if(ch2 == '-'){a[i][2] = -1;}
        else { a[i][2] = ch2-'0'; a[ch2-'0'][0] = 1;}
        //a[i*2+1][0] = stoi(ch2);
        //a[i*2][0] = stoi(ch1);
        //不能这样写,因为这是树的存储,判断是否是一个父节点,而不是关系的存储。
        //来判断是否是一个父节点。
    }

       //dfs(i-1, i);
       //dfs 从头到尾遍历一遍???——wrong——如何实现——原因:思路不清晰
       //要完全认清楚:存好树的根节点与子节点,
       //遍历之前存的树,利用完全二叉树的性质:节点的下标从1-n-1, 深搜遍历。
       int i = 0;
       while(a[i][0]!=-1) i++;
        //为什么从最开始找到的就是根???——找到原因了,a[i][0]的意义

       dfs(i, 1); //dfs(a[i][0], i);——wrong原因就是:分清楚存储节点
                                                        //和深搜判断 index 的关系——不要混为一谈
        if(maxi == n)
            cout << "YES " << ans;
        else cout << "NO " << i;

        return 0;
}

//修改后AC代码


//char->改成string , stoi()函数时就可以通过!!!
#include<bits/stdc++.h>
#include<cstring>
#define MAXN 105
using namespace std;


int maxi = -1, ans;
int a[MAXN][5]; //记录0-n-1 个节点是不是根,左右孩子是谁

void dfs(int root, int index){  //root为节点,index为当前节点下标值
                                //深搜的契合性——由根依次深入,直到判断到最远节点
                                //——index是统计的一个标志,树是由当前节点决定子树是谁
    if(index > maxi){
        maxi = index;
        ans = root;
    }

    if(a[root][1]!=-1) dfs(a[root][1], index*2);  //即从树的当前节点root往下遍历
    if(a[root][2]!=-1) dfs(a[root][2], index*2+1);
}

int main(){
    int n;
    cin >> n;
    string ch1, ch2;
    for(int i = 0; i <= n; i++) a[i][0] = -1;
    for(int i = 0; i < n; i++){ //0-n-1的节点

        //a[i][0]用来判断标记是否为根节点——只有一个!!!, a[i][1]左孩子值, a[i][2]右孩子值。i代表下标值。
        cin >> ch1 >> ch2;
        if(ch1 == "-"){a[i][1] = -1;}
        else { a[i][1] = stoi(ch1); a[stoi(ch1)][0] = 1;}

        if(ch2 == "-"){a[i][2] = -1;}
        else { a[i][2] = stoi(ch2); a[stoi(ch2)][0] = 1;}


        //atoi()用法
        /*if(ch1 == "-"){a[i][1] = -1;}
        else { a[i][1] = atoi(ch1.c_str()); a[atoi(ch1.c_str())][0] = 1;}

        if(ch2 == "-"){a[i][2] = -1;}
        else { a[i][2] = atoi(ch2.c_str()); a[atoi(ch2.c_str())][0] = 1;}*/



        //a[i*2+1][0] = stoi(ch2);
        //a[i*2][0] = stoi(ch1);
        //不能这样写,因为这是树的存储,判断是否是一个父节点,而不是关系的存储。
        //来判断是否是一个父节点。
    }

       //dfs(i-1, i);
       //dfs 从头到尾遍历一遍???——wrong——如何实现——原因:思路不清晰
       //要完全认清楚:存好树的根节点与子节点,
       //遍历之前存的树,利用完全二叉树的性质:节点的下标从1-n-1, 深搜遍历。
       int i = 0;
       while(a[i][0]!=-1) i++;
        //为什么从最开始找到的就是根???——找到原因了,a[i][0]的意义

       dfs(i, 1); //dfs(a[i][0], i);——wrong原因就是:分清楚存储节点
                                                        //和深搜判断 index 的关系——不要混为一谈
        if(maxi == n)
            cout << "YES " << ans;
        else cout << "NO " << i;

        return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值