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],

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

return its depth = 3.

  1. package leetcode.easy;
  2.  
  3. /**
  4. * Definition for a binary tree node. public class TreeNode { int val; TreeNode
  5. * left; TreeNode right; TreeNode(int x) { val = x; } }
  6. */
  7. public class MaximumDepthOfBinaryTree {
  8. public int maxDepth(TreeNode root) {
  9. if (null == root) {
  10. return 0;
  11. } else if (null == root.left) {
  12. return 1 + maxDepth(root.right);
  13. } else if (null == root.right) {
  14. return 1 + maxDepth(root.left);
  15. } else {
  16. return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
  17. }
  18. }
  19.  
  20. @org.junit.Test
  21. public void test() {
  22. TreeNode tn11 = new TreeNode(3);
  23. TreeNode tn21 = new TreeNode(9);
  24. TreeNode tn22 = new TreeNode(20);
  25. TreeNode tn33 = new TreeNode(15);
  26. TreeNode tn34 = new TreeNode(7);
  27. tn11.left = tn21;
  28. tn11.right = tn22;
  29. tn21.left = null;
  30. tn21.right = null;
  31. tn22.left = tn33;
  32. tn22.right = tn34;
  33. tn33.left = null;
  34. tn33.right = null;
  35. tn34.left = null;
  36. tn34.right = null;
  37. System.out.println(maxDepth(tn11));
  38. }
  39. }

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. 修改httpd端口

    修改httpd端口 默认httpd端口为80,现在改成800 修改两个地方: 1.修改配置文件httpd.conf listen 把80改成需要的端口 2.修改配置文件httpd-vhosts.con ...

  2. 【Java】《Java程序设计基础教程》第一章学习

    一.Java概述 1.介绍了Java语言的由来 2.Java语言的特点:简单.面向对象.分布式.高效解释执行.健壮.安全.结构中立.可移植.高效率.多线程.动态 3.Java语言的实现机制,引入虚拟机 ...

  3. python_面向对象——封装

    1.私有属性 class Person(object): def __init__(self,name,age): self.name = name self.age = age #实例属性 self ...

  4. 用CSS 实现 浮动元素的 水平居中

    问题描述: 基本的html结构: <div> <!-- <span>1</span> <span>2</span> <span& ...

  5. VMWARE许可文件

    VMware 14 Pro 永久许可证激活密钥FF31K-AHZD1-H8ETZ-8WWEZ-WUUVACV7T2-6WY5Q-48EWP-ZXY7X-QGUWD

  6. spring security控制session

    spring security控制session本文给你描述在spring security中如何控制http session.包括session超时.启用并发session以及其他高级安全配置. 创 ...

  7. ORA-00600[2662]问题 汇总

    一.ORA-00600[2662]问题模拟及解决方法 这是2013年的一篇文章,也不知道数据库是什么版本, 我的数据库时11.2.0.4,使用下面方法模拟的时候,模拟不出来....   参照eygle ...

  8. js中 0.1+0.2 !== 0.3

    1. 存储原理: 在计算机中数字无论是定点数还是浮点数都是以多位二进制的方式进行存储的.事实上不仅仅是 Javascript,在很多语言中 0.1 + 0.2 都会得到 0.3000000000000 ...

  9. 快速上手mpvue 项目

    初始化一个 mpvue 项目 $ node -v v8.9.0 $ npm -v 5.6.0 # 2. 由于众所周知的原因,可以考虑切换源为 taobao 源 $ npm set registry h ...

  10. Python中一些高效的数据操作

    列表统计 chars = ["a", "b", "a", "c", "a", "d&quo ...