zoukankan      html  css  js  c++  java
  • 二叉树的深度

    多的不解释了,这里有两种解法。

    第一种:一般的解法

     1 void Deep(BinaryTreeNode* root , int& Maxdeep , int count)
     2 {
     3     if (root->m_pLeft || root->m_pRight)
     4     {
     5         count++;
     6         if (root->m_pLeft)
     7         {
     8             Deep(root->m_pLeft , Maxdeep,count);
     9         }
    10         if (root->m_pRight)
    11         {
    12             Deep(root->m_pRight , Maxdeep,count);
    13         }
    14         
    15     }else //遍历到叶节点
    16     {
    17         count++;
    18         if (count > Maxdeep)
    19         {
    20             Maxdeep = count ;//保存最大深度
    21         }
    22     }
    23 }
    24 int TreeDepth(BinaryTreeNode* root)
    25 {
    26     int Maxdeep = 0 ;
    27     int count = 0 ;
    28     Deep(root, Maxdeep, count);
    29     return Maxdeep ;
    30 }

    第二种:大神的解法,代码简洁高效

     1 int TreeDepth(BinaryTreeNode* root)
     2 {
     3     if (root == NULL)
     4     {
     5         return 0 ;
     6     }
     7     int left = TreeDepth(root->m_pLeft);
     8     int right = TreeDepth(root->m_pRight);
     9     return (left > right) ? (left+1) : (right + 1) ;
    10 }
  • 相关阅读:
    charles使用
    断言
    JDBC Request
    HTTP请求建立一个测试计划
    利用badboy进行脚本录制
    接口测试用例
    Monkey常用命令
    charles安装与使用
    celery配置与基本使用
    图片验证码接口
  • 原文地址:https://www.cnblogs.com/csxcode/p/3735419.html
Copyright © 2011-2022 走看看