要求

  • 求一棵二叉树的最高深度

思路

  • 递归地求左右子树的最高深度

实现

 1 Definition for a binary tree node.
2 struct TreeNode {
3 int val;
4 TreeNode *left;
5 TreeNode *right;
6 TreeNode(int x) : val(x), left(NULL), right(NULL) {}
7 };
8
9 class Solution {
10 public:
11 int maxDepth(TreeNode* root) {
12
13 if( root == NULL )
14 return 0;
15
16 return max( maxDepth( root->left ),maxDepth( root->right )) + 1;
17 }
18 };

相关

  • 111 Minimum Depth of Binary Tree

[刷题] 104 Maximum Depth of Binary Tree的更多相关文章

  1. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

  2. 104. Maximum Depth of Binary Tree(C++)

    104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...

  3. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  4. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

  5. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

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

  7. (二叉树 BFS DFS) 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. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  9. 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 ...

随机推荐

  1. 全网最详细的Linux命令系列-cat命令

    cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 命令格式: cat [选项] [文件].. ...

  2. 对不起,“下一代ERP”仍旧是现在的ERP

    最近数字化转型太火了,到处都是相关数字化的网文.很多人又说在数字化转型时代,ERP早就落伍了云云,取而代之的是什么"下一代ERP",叫什么"ARP"." ...

  3. 【算法学习笔记】Meissel-Lehmer 算法 (亚线性时间找出素数个数)

    「Meissel-Lehmer 算法」是一种能在亚线性时间复杂度内求出 \(1\sim n\) 内质数个数的一种算法. 在看素数相关论文时发现了这个算法,论文链接:Here. 算法的细节来自 OI w ...

  4. 1149 Dangerous Goods Packaging

    When shipping goods with containers, we have to be careful not to pack some incompatible goods into ...

  5. NumPy之:结构化数组详解

    目录 简介 结构化数组中的字段field 结构化数据类型 创建结构化数据类型 从元组创建 从逗号分割的dtype创建 从字典创建 操作结构化数据类型 Offsets 和Alignment Field ...

  6. 1.4.18- HTML之特殊字符标签

    上面图片内容简单了解一下,下面我们看一段代码: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  7. php的call_user_func_array()使用场景

    1..动态调用普通函数时,比如参数和调用方法名称不确定的时候很好用 function sayEnglish($fName, $content) { echo 'I am ' . $content; } ...

  8. ubuntu14.04忽然不能登录,输入密码一直返回登录界面

    解决方法: 1.ctrl + alt + F1进入命令终端 2.重装gdm,sudo apt-get install gdm 3.修改启动顺序:dpkg -reconfigure gdm 4.重启re ...

  9. hdu2604 矩阵快速幂

    题意:      给你n个人,排成一个长度是n的队伍,人只有两类f,m,问可以有多少种排法使度列中不出现fff,fmf这样的子串.思路:      一开始暴力,结果超时了,其实这个题目要是能找到类似于 ...

  10. hdu3374最小表示法+KMP

    题意:       给你一个最长100W的串,然后让你找到最小同构子串,还有最大同构子串的下标,最小同构子串就是把字符串连接成一个环,然后选择一个地方断开,得到的一个ASCII最小的子串(求最大同理) ...