LeetCode_104. Maximum Depth of Binary Tree
104. Maximum Depth of Binary Tree
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的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【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 ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
随机推荐
- 随便写一个c++类
为了让代码更贴合实际项目需要,我们分别用xxx.h文件,xxx.cpp文件来包含类的定义,类的声明和类的调用部分,实验平台vs2010 mycoach.h文件 #pragma once #includ ...
- LightOJ - 1148-Mad Counting (数学)
链接: https://vjudge.net/problem/LightOJ-1148 题意: Mob was hijacked by the mayor of the Town "Trut ...
- AirtestIde的安装(win10)
Airtest 是网易出品的一款基于图像识别和poco控件识别的一款UI自动化测试工具. Airtest IDE是这个项目的一个IDE,就像Eclipse.Pycharm一样,是一个集成开发工具. A ...
- HTML 008 head
HTML <head> 查看在线实例 <title> - 定义了HTML文档的标题使用 <title> 标签定义HTML文档的标题 <base> - 定 ...
- php面向对象之静态方法使用
什么是静态方法? 不是所有的变量和方法都要通过创建对象来调用.可以通过给变量和方法加上static关键字来直接调用.无锡大理石测量平台 调用静态成员的语法格式为: 关键字::静态成员 关键字可以是: ...
- The backup set holds a backup of a database other than the existing ‘dbName’ database
[Solved] System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than t ...
- public abstract啥时候可以省略?
父类是抽象类,其中有抽象方法,那么子类继承父类,并把父类中的所有方法都实现覆盖了,子类才有创建对象实例的能力,否则子类也必须是抽象类.抽象类中可以有构造方法,是子类在构造子类对象时需要调用父类(抽象类 ...
- $noip2018$游记+考后总结
游记部分 Day-5 - Day0 敲了敲模板,打了几场模拟赛,都在颓废,其他什么都没做 Day1 早上继续写模板,水了会QQ,其他什么都没做 中午试图背模板,没成功(其实下午才发现敲的模板一个没用上 ...
- scrapy 分布式爬虫- RedisSpider
爬去当当书籍信息 多台机器同时爬取,共用一个redis记录 scrapy_redis 带爬取的request对象储存在redis中,每台机器读取request对象并删除记录,经行爬取.实现分布式爬虫 ...
- 洛谷P1419寻找段落
题目 单调队列+前缀和 #include <bits/stdc++.h> #define N 101001 using namespace std; int n, s, t; int da ...