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 ...
随机推荐
- 修改httpd端口
修改httpd端口 默认httpd端口为80,现在改成800 修改两个地方: 1.修改配置文件httpd.conf listen 把80改成需要的端口 2.修改配置文件httpd-vhosts.con ...
- 【Java】《Java程序设计基础教程》第一章学习
一.Java概述 1.介绍了Java语言的由来 2.Java语言的特点:简单.面向对象.分布式.高效解释执行.健壮.安全.结构中立.可移植.高效率.多线程.动态 3.Java语言的实现机制,引入虚拟机 ...
- python_面向对象——封装
1.私有属性 class Person(object): def __init__(self,name,age): self.name = name self.age = age #实例属性 self ...
- 用CSS 实现 浮动元素的 水平居中
问题描述: 基本的html结构: <div> <!-- <span>1</span> <span>2</span> <span& ...
- VMWARE许可文件
VMware 14 Pro 永久许可证激活密钥FF31K-AHZD1-H8ETZ-8WWEZ-WUUVACV7T2-6WY5Q-48EWP-ZXY7X-QGUWD
- spring security控制session
spring security控制session本文给你描述在spring security中如何控制http session.包括session超时.启用并发session以及其他高级安全配置. 创 ...
- ORA-00600[2662]问题 汇总
一.ORA-00600[2662]问题模拟及解决方法 这是2013年的一篇文章,也不知道数据库是什么版本, 我的数据库时11.2.0.4,使用下面方法模拟的时候,模拟不出来.... 参照eygle ...
- js中 0.1+0.2 !== 0.3
1. 存储原理: 在计算机中数字无论是定点数还是浮点数都是以多位二进制的方式进行存储的.事实上不仅仅是 Javascript,在很多语言中 0.1 + 0.2 都会得到 0.3000000000000 ...
- 快速上手mpvue 项目
初始化一个 mpvue 项目 $ node -v v8.9.0 $ npm -v 5.6.0 # 2. 由于众所周知的原因,可以考虑切换源为 taobao 源 $ npm set registry h ...
- Python中一些高效的数据操作
列表统计 chars = ["a", "b", "a", "c", "a", "d&quo ...