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.

求树的高度。

深度优先搜索遍历。递归实现。

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

leetcode 104 Maximum Depth of Binary Tree ----- java的更多相关文章

  1. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

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

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

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

  4. (二叉树 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 ...

  5. LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java

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

  6. Java for 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. Java [Leetcode 104]Maximum Depth of Binary Tree

    题目描述: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th ...

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

  9. LeetCode 104. Maximum Depth of Binary Tree

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

随机推荐

  1. 【未完成0.0】Noip2012提高组day2 解题报告

    第一次写一套题的解题报告,感觉会比较长.(更新中Loading....):) 题目: 第一题:同余方程 描述 求关于x的同余方程ax ≡ 1 (mod b)的最小正整数解. 格式 输入格式 输入只有一 ...

  2. MyEclipse的 at com.genuitec.eclipse.ast.deploy.core.Deployment.<init>错误解决办法

    我们使用myeclipse自动部署web项目时会设置服务器的位置,而部署的相关配置会存储在myeclipse工作目录下的“/.metadata/.plugins/org.eclipse.core.ru ...

  3. Section 1.4 The Clocks

    0 0 虽然不知不觉做到了Section 1.4了,但是都没有把做题的想法和代码发到这里… 本来今天想从Section 1.2补起来然后发现之前做的题都忘了…(Name That Number那道题是 ...

  4. Java选择结构、循环结构

    1:switch语句(掌握) (1)格式: switch(表达式) { case 值1: 语句体1; break; case 值2: 语句体2; break; ... default: 语句体n+1; ...

  5. 生成1~n的全排列

    输入正整数n,输出n的全排列. 样例输入1: 3 样例输出1: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 分析: 按字典序从小到大的顺序输出所有的排列. (字典序:两个序 ...

  6. 技术分享:逆向分析ATM分离器

    文章内容仅供技术交流,请勿模仿操作! 背景(作者) 每一次外出时, Elizabeth和我总是格外的小心,同时把我们身上的钱藏在特殊的皮带上面,这样还不够,我们还采取了“狡兔三窟”的方式来藏身上带的银 ...

  7. ios学习之 关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系

    刚接触iOS开发的人难免会对苹果的各种证书.配置文件等不甚了解,可能你按照网上的教程一步一步的成功申请了真机调试,但是还是对其中的缘由一知半解.这篇文章就对Certificate.Provisioni ...

  8. The Blocks Problem

    Description Many areas of Computer Science use simple, abstract domains for both analytical and empi ...

  9. Android点击View显示PopupWindow,再次重复点击View关闭PopupWindow

     Android点击View显示PopupWindow,再次重复点击View关闭PopupWindow 这本身是一个看似很简单的问题,但是如果设置不当,就可能导致莫名其妙失效问题.通常在Andro ...

  10. WPF Step By Step 系列 - 开篇 ·

    WPF Step By Step 系列 - 开篇 公司最近要去我去整理出一个完整的WPF培训的教程,我刚好将自己学习WPF的过程和经验总结整理成笔记的方式来讲述,这里就不按照书上面的东西来说了,书本上 ...