Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its depth = 3.

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
return helper(root);
} private int helper(TreeNode root) {
if (root == null) {
return 0;
}
int left = helper(root.left);
int right = helper(root.right);
return Math.max(left, right) + 1;
}
}

[LC] 104. Maximum Depth of Binary Tree的更多相关文章

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

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

  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. [刷题] 104 Maximum Depth of Binary Tree

    要求 求一棵二叉树的最高深度 思路 递归地求左右子树的最高深度 实现 1 Definition for a binary tree node. 2 struct TreeNode { 3 int va ...

随机推荐

  1. I420转RGB

    http://blog.csdn.net/huiguixian/article/details/17288909 public class YuvToRGB { private static int  ...

  2. nginx四层负载及动静分离

    阿里云实验10.0.0.132 LB10.0.0.133 web0110.0.0.134 web02 步骤:1.安装nginx1.14 获取官网repo文件 yum install -y nginx ...

  3. gff文件提取cds

    #!/usr/bin/perl use strict; use warnings; ########input######## ];my $cut = &cut($gff);my %cut = ...

  4. [NOI2014]魔法森林(LCT)

    蒟蒻又发现自己还没写过LCT…… 首先显然按照权值a从小到大排序,维护b的最小生成树.然后直接扫,代价是加入b的最大值,然后动态加边,因为有边权,所以在lct中边应该理解为点.每次连接(u,v)时,若 ...

  5. Codeforces Round #624 (Div. 3)(题解)

    A. Add Odd or Subtract Even 思路: 相同直接为0,如果两数相差为偶数就为2,奇数就为1 #include<iostream> #include<algor ...

  6. 吴裕雄--天生自然 pythonTensorFlow自然语言处理:Seq2Seq模型--测试

    import sys import codecs import tensorflow as tf # 1.参数设置. # 读取checkpoint的路径.9000表示是训练程序在第9000步保存的ch ...

  7. rsync配置文件模板

    用脚本实现服务端rsyncd的部署cat /server/scripts/rsync_install.sh #!/bin/bash #安装包 yum install -y rsync &> ...

  8. iOS雪花动画、音频图、新闻界面框架、2048游戏、二维码条形码扫码生成等源码

    iOS精选源码 粒子雪花与烟花的动画 iOS 2048游戏 JHSoundWaveView - 简单地声波图.音波图 一个可快速集成的新闻详情界面框架,类似今日头条,腾讯新闻 二维码/条形码扫描及扫描 ...

  9. python,pandas, DataFrame数据获取方式

    一.创建DataFrame df=pd.DataFrame(np.arange(,).reshape(,)) my_col=dict(zip(range(),['A','B','C'])) df.re ...

  10. 关于guava实现线程池

    private ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCac ...