题目描述

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例

给定二叉树 [3,9,20,null,null,15,7]

   / \

    /  \
      

返回它的最大深度 3 。

题目要求

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/ int maxDepth(struct TreeNode* root){ }

题解

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/ int _max(int a,int b){
return a>b?a:b;
} int maxDepth(struct TreeNode* root){
if(root==NULL)return ;
/*
加这三行会快一些,在数据量小的时候(上限高)
if(root->left==NULL&&root->right==NULL)return 1;
else if(root->left==NULL)return maxDepth(root->right)+1;
else if(root->right==NULL)return maxDepth(root->left)+1;
*/
return +_max(maxDepth(root->left),maxDepth(root->right));
}

签到递归题,递归题只要情况考虑周到了,尤其是根节点为空的情况,就应该不会写错。

题目来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

C语言递归之二叉树的最大深度的更多相关文章

  1. C语言递归实现二叉树(二叉链表)的三种遍历和销毁操作(实验)

    今天写的是二叉树操作的实验,这个实验有三个部分: ①建立二叉树,采用二叉链表结构 ②先序.中序.后续遍历二叉树,输出节点值 ③销毁二叉树 二叉树的节点结构定义 typedef struct BiTNo ...

  2. C语言递归之二叉树的最小深度

    题目描述 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例 输入:[3,9,20,null,null,15,7] ...

  3. C语言递归实现二叉树的先序、中序、后序遍历

    #include <stdio.h> #include <stdlib.h> //*****二叉树的二叉链表存储表示*****// typedef struct BiNode ...

  4. 【Lintcode】二叉树的最大深度 - 比较简单,用递归比较好,不递归也能做,比较麻烦

    给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的距离. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5 这个二叉树的 ...

  5. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. lintcode :二叉树的最大深度

    题目: 二叉树的最大深度 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的距离. 样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5 这个二叉树的最大深度为3. 解 ...

  7. Leetcode 104. Maximum Depth of Binary Tree(二叉树的最大深度)

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  8. 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度

    求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  9. LeetCode(104):二叉树的最大深度

    Easy! 题目描述: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null, ...

随机推荐

  1. BZOJ 2458: [BeiJing2011]最小三角形 (分治)

    分治就是了. 类似于分治找最近/远点对. CODE #include <bits/stdc++.h> using namespace std; const double eps = 1e- ...

  2. Maven 配置问题 - could not find resource mybatis-config.xml

    需要在pom中加入以下代码 <build> <resources> <resource> <directory>src/main/resources&l ...

  3. 包过滤防火墙iptables(网络层)

    安装: yum -y install iptables-services 启动:systemctl start iptables.service 四表五链 过滤:filter - input forw ...

  4. Delphi 实现Ping命令

    Delphi  实现Ping命令 unit FtPing; interface uses Windows, SysUtils, Classes, Controls, Winsock, StdCtrls ...

  5. 数据结构实验之链表五:单链表的拆分(SDUT 2120)

    #include <bits/stdc++.h> using namespace std; struct node { int data; struct node *next; }; st ...

  6. 用python来抓取“煎蛋网”上面的美女图片,尺度很大哦!哈哈

    所用Python环境为:python 3.3.2   用到的库为:urllib.request    re 废话不多说,先上代码: import urllib.request import re #获 ...

  7. 在ABP core中使用RabbitMq

    距上一篇博客的更新一集很久了,主要是最近做的事情比较杂,中间也有一个难点,就是在ABP中加入APP扫码登录,本来想些的,但是觉得这个写出来会不会让我们的系统被破解-_-||,所以想了想,就没有写. 这 ...

  8. ARTS打卡计划第十六周

    Algorithms: https://leetcode-cn.com/problems/min-stack/submissions// Review: https://www.infoq.cn/ar ...

  9. .net 数据导出

    安装npoi,下面是具体的C#代码: public static XSSFWorkbook BuildWorkbook(DataTable dt) { var book = new XSSFWorkb ...

  10. 用itext生成PDF报错:Font 'STSong-Light1' with 'UniGB-UCS2-H' is not recognized.

    用itext生成PDF报错,加上try catch捕获到异常是 BaseFont bFont = BaseFont.createFont("STSong-Light1", &quo ...