zoukankan      html  css  js  c++  java
  • leetcode-104.二叉树最大深度 · BTree + 递归

    easy 题就不详细叙述题面和样例了,见谅。

    题面

    统计二叉树的最大深度。

    算法

    递归搜索二叉树,返回左右子树的最大深度。

    源码

     1 class Solution {
     2 public:
     3     int maxDepth(TreeNode* root) {
     4         if(root == nullptr)
     5             return 0;
     6         //根节点算一层
     7         int res = 1;
     8         return max(getDepth(root->left, res), getDepth(root->right, res));//递归
     9     }
    10     
    11     int getDepth(TreeNode* p, int res)
    12     {
    13         if(p == nullptr)//叶子节点返回结果
    14             return res;
    15         else
    16             return max(getDepth(p->left, res+1), getDepth(p->right, res+1));//递归
    17     }
    18 };
  • 相关阅读:
    3.24课堂
    3.23作业
    3.23课堂
    3.20作业
    3.20课堂
    3.19作业
    3.19课堂
    3.18课堂
    3.18作业
    46、表与表的连接
  • 原文地址:https://www.cnblogs.com/yocichen/p/10905033.html
Copyright © 2011-2022 走看看