zoukankan      html  css  js  c++  java
  • 南大oj非完美二叉树的直径和高度

    记T为一棵二叉树,树中共有n个节点。

    定义根节点的深度为0,其余节点的深度为其父节点的深度加1。T的高度定义为其叶节点深度的最大值。

    定义树中任意两点a和b之间的距离为其间最短简单路径的长度。T的直径定义为T中所有点对间距离的最大值。

    输入一棵二叉树T,请计算它的高度和直径。

    思路:高度直接递归,南大定义根节点高度为0,所以要减去1(南大的定义不正常);

    直径不是一般意义的宽度,是任意两个结点深度优先遍历的最大值,所以这题可以转化为图用DFS做,更简单的是不要建树用分治法做

    我这里用思路简单的递归做吧

    当跟左右子树都不空:width=显然是左子树+右子树深度+2;

    当是其他情况:width=根节点的孩子结点的的宽度和父节点高度的最大值;

    #include<cstdio>
    #include<iostream>
    #include<stdlib.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    typedef struct BTree{
        int data;
        BTree *lchild;
        BTree *rchild;
        BTree *parent;
        
    }BiTree;
    int num=0;
    int A[30];//先序
    int B[30];//中序
    int N;
    
    
    queue<int> res;
    void levelorder(BiTree * root)
    {
        if(root==NULL)return;
        BiTree* temp;
        queue<BiTree*> q;
        q.push(root);
        while(!q.empty())
        {
            temp=q.front();
           // res.push(temp->data);
            q.pop();
          //  printf("%d",temp->data); 
          //  num++;
           // if(num<N)printf(" ");
            if(temp->lchild)
            {
                q.push(temp->lchild);
            }
            if(temp->rchild)
            {
                q.push(temp->rchild);
            }
        }
    }
    int Height(BiTree* root)
    {
        if(root==NULL)return 0;
        int leftH=Height(root->lchild);
        int rightH=Height(root->rchild);
        return max(leftH,rightH)+1;
    }
    
    
    BiTree* create(int preL,int preR,int inL,int inR)
    {
        if(preL<=preR)
        {
             BiTree * root=(BiTree*)malloc(sizeof(BiTree));
       // root->parent=NULL;
        root->data=A[preL];
        int i;
        for(i=inL;B[i]!=root->data;i++);
        int leftLen=i-inL;
        int rightLen=inR-i;
            if(leftLen)
            {
            root->lchild=create(preL+1,preL+leftLen,inL,inL+leftLen-1);
            //root->lchild->parent=root; 
            }
            else
            {
                root->lchild=NULL;
            }
            if(rightLen)
            {
                root->rchild=create(preR-rightLen+1,preR,inR-rightLen+1,inR);
          // root->rchild->parent=root; 
            }
            else
            {
                root->rchild=NULL;
            }
       
            return root;
        }
       
            
        
        
    }
    int width(BiTree*root)
    {
    
        if(root==NULL)return 0;
        if(root->lchild&&root->rchild)return Height(root->lchild)-1+Height(root->rchild)-1+2;
    //    if(root->lchild==NULL&&root->rchild==NULL);return 0;
    //    return root->lchild?width(root->lchild)+1:width(root->rchild)+1;
    //printf("root:%d",Height(root)-1);
        return root->lchild?max(Height(root)-1,width(root->lchild)):max(Height(root)-1,width(root->rchild));
    }
    int main()
    {
        //int x[11]={0,1,9,3,8,4,2,7,5,6};
        //int y[11]={3,9,8,1,2,4,0,5,7,6};
    //int x[11]={1,9,3,8,4,2};
    //int y[11]={3,9,8,1,2,4};
    //    for(int i=0;i<10;i++)
    //    {
    //        A[i]=x[i];
    //        B[i]=y[i];
    //    }
    scanf("%d",&N);
    for(int i=0;i<N;i++)
    scanf("%d",&A[i]);
    for(int i=0;i<N;i++)
    scanf("%d",&B[i]);
    //N=10;
        BiTree* root=create(0,N-1,0,N-1);
        levelorder(root);
       // cout<<endl;
        printf("I have read the rules about plagiarism punishment
    ");
        cout<<Height(root)-1<<endl;
        cout<<width(root)<<endl;
    
    //while(!res.empty())
    //{
    //    cout<<res.front()<<" ";
    //    res.pop();
    //}
    //10
    //0 1 9 3 8 4 2 7 5 6
    //3 9 8 1 2 4 0 5 7 6
    
    // 1 9 3 8 4 2
    // 3 9 8 1 2 4
        return 0;
    }
  • 相关阅读:
    Sun开发的JINI技术在网络中的应用
    让memcached和mysql更好的工作
    Nginx+Tomcat+memcached负载均衡实现session共享
    Nginx 简单的负载均衡配置示例
    数据库sharding(scale up to scale out)
    docker专题(2):docker常用管理命令(上)
    UMDF
    编程精粹:编写高质量的C语言代码———笔记一
    子矩阵中共享1的最长对角线
    Print the numbers between 30 to 3000.
  • 原文地址:https://www.cnblogs.com/tianyudizhua/p/13488394.html
Copyright © 2011-2022 走看看