104. Maximum Depth of Binary Tree

Easy

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.

package leetcode.easy;

/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class MaximumDepthOfBinaryTree {
public int maxDepth(TreeNode root) {
if (null == root) {
return 0;
} else if (null == root.left) {
return 1 + maxDepth(root.right);
} else if (null == root.right) {
return 1 + maxDepth(root.left);
} else {
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
} @org.junit.Test
public void test() {
TreeNode tn11 = new TreeNode(3);
TreeNode tn21 = new TreeNode(9);
TreeNode tn22 = new TreeNode(20);
TreeNode tn33 = new TreeNode(15);
TreeNode tn34 = new TreeNode(7);
tn11.left = tn21;
tn11.right = tn22;
tn21.left = null;
tn21.right = null;
tn22.left = tn33;
tn22.right = tn34;
tn33.left = null;
tn33.right = null;
tn34.left = null;
tn34.right = null;
System.out.println(maxDepth(tn11));
}
}

LeetCode_104. Maximum Depth of Binary Tree的更多相关文章

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

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

  2. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  3. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  4. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  5. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  6. Leetcode | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

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

  8. 【LeetCode练习题】Maximum Depth of Binary Tree

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

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

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

随机推荐

  1. 神经网络(2)---neurons and the brain

    神经网络是一个历史悠久的课题,当初提出是为了让机器能够模仿人的大脑一样工作.现在神经网络在处理很多机器学习的问题上发挥了很重要的作用. 神经网络的背景(我们可以利用神经网络做些什么) 大脑可以处理视觉 ...

  2. 2019-2020-1 20199312《Linux内核原理与分析》第一周作业

    实验一:linux系统简介 Linux 本身只是操作系统的内核.内核是使其它程序能够运行的基础.它实现了多任务和硬件管理,用户或者系统管理员交互运行的所有程序实际上都运行在内核之上.其中有些程序是必需 ...

  3. Substring Anagrams

    Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...

  4. Oracle的dual是什么东西啊??(答:伪表)

    1. dual 确实是一张表.是一张只有一个字段,一行记录的表.2.习惯上,我们称之为'伪表'.因为他不存储主题数据.3. 他的存在,是为了操作上的方便.因为select 都是要有特定对象的.如:se ...

  5. vue 组件的通信方式(完整版)

    几种通信方式无外乎以下几种: Prop(常用) $emit (组件封装用的较多) .sync语法糖 (较少) $attrs & $listeners (组件封装用的较多) provide &a ...

  6. python 元组 【基本使用功能】

    元组是括号,列表是方括号,都可以通用的有好多,比如判断一个元素是否存在可以直接用 in ,复制或者合并可以直接用乘或者加. 下面是在菜鸟教程截得的: 示例: #!/usr/bin/python # - ...

  7. 1827:【01NOIP提高组】Car的旅行路线

    哇这些真题终于正经起来奥 刚看这道题很不自信觉得自己肯定不能建图成功甚至想过用贪心.. 后来一想发现建图还是蛮容易的,AI我是真的蠢 话说一本通真的很坑啊,把原题的保留1位改成了2 我把在洛谷AC的代 ...

  8. Python豆瓣书籍信息爬虫

    练习下BeautifulSoup,requests库,用python3.3 写了一个简易的豆瓣小爬虫,将爬取的信息在控制台输出并且写入文件中. 上源码: # coding = utf-8 '''my ...

  9. Anriod手机抓包

    1. 下载Fiddler http://fiddler2.com/ 2. 开启Fiddler的远程连接,Fiddler 主菜单 Tools -> Fiddler Options--> Co ...

  10. Telegraf+Influxdb+Grafana(Windows下本机简易监控系统搭建)

    1.文件名称 telegraf-1.5.0_windows_amd64.zip influxdb-1.4.2_windows_amd64.zip grafana-4.6.3.windows-x64.z ...