zoukankan      html  css  js  c++  java
  • 【IT笔试面试题整理】 二叉树任意两个节点间最大距离

    求一个二叉树中任意两个节点间的最大距离,两个节点的距离的定义是这两个节点间边的个数, 比如某个孩子节点和父节点间的距离是1,和相邻兄弟节点间的距离是2,优化时间空间复杂度。

    一种是:经过根节点,此时只需要求出左右子树的最大深度就可以

    另一种:不经过根节点,此时需要递归求解左右子树,然后比较左右子树中最大距离,求大者

     

      1  #include "stdio.h"
      2  #include"stdlib.h" 
      3  struct NODE
      4     {
      5          NODE* pLeft;            // 左子树
      6          NODE* pRight;          // 右子树
      7          int nMaxLeft;          // 左子树中的最长距离
      8          int nMaxRight;         // 右子树中的最长距离
      9          int chValue;        // 该节点的值
     10     };
     11    
     12     int nMaxLen = 0;
     13    
     14     // 寻找树中最长的两段距离
     15     void FindMaxLen(NODE* pRoot)
     16     {
     17          // 遍历到叶子节点,返回
     18          if(pRoot == NULL)
     19               return;
     20  
     21          // 如果左子树为空,那么该节点的左边最长距离为0
     22          if(pRoot -> pLeft == NULL)   
     23               pRoot -> nMaxLeft = 0;
     24          
     25    
     26          // 如果右子树为空,那么该节点的右边最长距离为0
     27          if(pRoot -> pRight == NULL)   
     28               pRoot -> nMaxRight = 0;
     29          
     30    
     31          // 如果左子树不为空,递归寻找左子树最长距离
     32          if(pRoot -> pLeft != NULL)       
     33               FindMaxLen(pRoot -> pLeft);
     34          
     35    
     36          // 如果右子树不为空,递归寻找右子树最长距离
     37          if(pRoot -> pRight != NULL)      
     38               FindMaxLen(pRoot -> pRight);
     39          
     40    
     41          // 计算左子树最长节点距离
     42          if(pRoot -> pLeft != NULL)
     43          {
     44               int nTempMax = 0;
     45               if(pRoot -> pLeft -> nMaxLeft > pRoot -> pLeft -> nMaxRight)
     46               {
     47                    nTempMax = pRoot -> pLeft -> nMaxLeft;
     48               }
     49               else
     50               {
     51                    nTempMax = pRoot -> pLeft -> nMaxRight;
     52               }
     53               pRoot -> nMaxLeft = nTempMax + 1;
     54          }
     55    
     56          // 计算右子树最长节点距离
     57          if(pRoot -> pRight != NULL)
     58          {
     59               int nTempMax = 0;
     60               if(pRoot -> pRight -> nMaxLeft > pRoot -> pRight -> nMaxRight)
     61               {
     62                    nTempMax = pRoot -> pRight -> nMaxLeft;
     63               }
     64               else
     65               {
     66                    nTempMax = pRoot -> pRight -> nMaxRight;
     67               }
     68               pRoot -> nMaxRight = nTempMax + 1;
     69          }
     70    
     71          // 更新最长距离
     72          if(pRoot -> nMaxLeft + pRoot -> nMaxRight > nMaxLen)
     73          {
     74               nMaxLen = pRoot -> nMaxLeft + pRoot -> nMaxRight;
     75          }
     76      }
     77          
     78 NODE *createTree()
     79 {
     80     NODE *root;
     81     int data;
     82     printf("input data:");
     83     scanf("%d",&data);
     84     //printf("output data:%d\n",data);
     85     
     86     if(data==0)
     87       root=NULL;
     88     else/*根左右 前序建立二叉树*/
     89     {
     90         root=(NODE*)malloc(sizeof(NODE));
     91         root->chValue=data;
     92         root->pLeft=createTree();
     93         root->pRight=createTree();    
     94     }
     95     return root;
     96 } 
     97 int main()
     98 {
     99     NODE  *root;
    100     root=createTree();
    101     FindMaxLen(root);
    102     
    103     printf("%d",nMaxLen);
    104     return 0;
    105 }
  • 相关阅读:
    rest framework 认证 权限 频率
    rest framework 视图,路由
    rest framework 序列化
    10.3 Vue 路由系统
    10.4 Vue 父子传值
    10.2 Vue 环境安装
    10.1 ES6 的新增特性以及简单语法
    Django 跨域请求处理
    20190827 On Java8 第十四章 流式编程
    20190825 On Java8 第十三章 函数式编程
  • 原文地址:https://www.cnblogs.com/WayneZeng/p/3034187.html
Copyright © 2011-2022 走看看