题意:给一棵二叉树,求其深度。

思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1。

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if(!root) return ;
int a=maxDepth(root->left);
int b=maxDepth(root->right);
return a>b?a+:b+;
}
};

AC代码

LeetCode Maximum Depth of Binary Tree (求树的深度)的更多相关文章

  1. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  2. [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度

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

  3. [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 ...

  4. [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 ...

  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. 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度

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

  7. [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

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

  8. leetcode Maximum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  9. leetcode:Maximum Depth of Binary Tree【Python版】

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...

随机推荐

  1. Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming

    Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming 在Spark2.x中,Spark Streaming获得了比较全面的升级,称为St ...

  2. Java中Map的用法详解

    Map简介 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值.此接口取代 Dictionary 类,后者完全是一个抽象类,而不是一个接口. Map 接口提供三种collecti ...

  3. 【socket】一分钟理清 socket udpsocket tcpsocket tcplistener TCPClient和 UDPClient

    socket 套接字接口是各种语言tcp udp的网络操作的基础. 直接用socket 对象开发 可以选择 udpsocket  或者 tcpsocket ,两者在使用上仅一些方法和参数不同,所有的底 ...

  4. 权限管理数据库设计_Rev1

    贴出来自身接触项目以来所接触过的一些企业管理信息系统权限部门的一个通用数据库设计初稿: 设计的文字解释以及各部分的作用等确定可行会再进行描述: 图: 如果有不同意见请轻拍!

  5. 一种高斯模糊渐变动画的实现-b

    关于高斯模糊的方式有很多种,但是如果需要模糊渐变,那么对这种高斯模糊算法的性能要求是比较高的,今天这里重点不讨论算法,只是提供一个动画实现的思路.动画效果如下: 高斯模糊渐变动画 //高斯模糊 -(U ...

  6. hibernate映射文件基础

    一.利用hibernate的eclipse插件快速生成实体类与配置文件的方法 1.首先下载安装和自己的eclipse版本配套的hibernate tools,如果是Myeclipse,在/readme ...

  7. 5.3:从bean的实例中获取对象

    5.3  从bean的实例中获取对象 在getBean方法中,getObjectForBeanInstance是个高频率使用的方法,无论是从缓存中获得bean还是根据不同的scope策略加载bean. ...

  8. C#中 Thread.Sleep精度问题

    Thread.Sleep的精度默认在15ms左右,如果需要类似 Thread.Sleep(1)的精细控制,需要调用特定的平台API实现 [DllImport("winmm.dll" ...

  9. 【搜索】BZOJ 3990: 【Sdoi 2015】排序

    3990: [SDOI2015]排序 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 336  Solved: 164[Submit][Status][ ...

  10. DX 的.x 文件

    template Header { <3D82AB43-62DA-11cf-AB39-0020AF71E433> WORD major; WORD minor; DWORD flags;} ...