zoukankan      html  css  js  c++  java
  • 求二叉树中节点的最大距离

    struct node
    {
        node *left;
        node *right;
        int maxleft;
        int maxright;
        char value;
    };
    int max=0;
    void findMaxLen(node *root)
    {
        if(root==NULL)
            return ;
        if(root->left=NULL)
            root->maxleft=0;
        if(root->right==NULL)
            root->maxright=0;
        if(root->left!=NULL)
            findMaxLen(root->left);
        if(root->right!=NULL)
            findMaxLen(root->right);
        //计算左子树最长节点距离
        if(root->left!=NULL)
        {
            int temp=0;
            if(root->left->maxleft>root->left->maxright)
                temp=root->left->maxleft;
            else
            {
                temp=root->left->maxright;
                
            }
            root->left->maxleft=temp+1;
        }
       //计算右子树最长节点距离
        if(root->right!=NULL)
        {
            int temp=0;
            if(root->right->maxleft>root->right->maxright)
                temp=root->right->maxleft;
            else
            {
                temp=root->right->maxright;
                
            }
            root->right->maxleft=temp+1;
        }
        //更新右子树最长节点的距离
        if(root->maxleft+root->maxright>max)
            max=root->maxleft+root->maxright;
        
    }
  • 相关阅读:
    Yii AR Model 查询
    学习进度4
    学习进度三
    个人每日总结7
    个人每日总结6
    个人每日总结5
    个人每日总结4
    个人冲刺承担的任务项目的用户模板和用户场景模板
    个人每日总结3
    个人每日总结2
  • 原文地址:https://www.cnblogs.com/wft1990/p/7603059.html
Copyright © 2011-2022 走看看