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.


题目标签:Tree
  这道题目给了我们一个二叉树,要我们找到最大深度,就是从root点到最深的那个点之间点的数量。利用post order 来遍历二叉树,对于每一个点,它的两个children会返回两个depth值,取一个大的, 加上1继续返回。当遍历到最深的level,当一个点node == null的时候,说明走到底了,返回0。
 
 

Java Solution:

Runtime beats 15.89%

完成日期:07/01/2017

关键词:Tree

关键点:post order 来遍历树; Math.max 来取一个更大的值(从两个children返回的depth)

 /**
* 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)
{
int depth = 0; if(root == null)
return depth; depth = Math.max(maxDepth(root.left), maxDepth(root.right)); return depth + 1;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)的更多相关文章

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

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

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

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

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

  5. Leetcode 104 Maximum Depth of Binary Tree 二叉树

    计算二叉树的最大深度 我的方法是找出两个子树的长度中最长的那个,然后加1 class Solution { public: int maxDepth(TreeNode* root) { ; ,maxD ...

  6. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

  7. 104 Maximum Depth of Binary Tree 二叉树的最大深度

    给定一个二叉树,找出其最大深度.二叉树的深度为根节点到最远叶节点的最长路径上的节点数.案例:给出二叉树 [3,9,20,null,null,15,7],    3   / \  9  20    /  ...

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

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

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

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

随机推荐

  1. 实际用户ID,有效用户ID及设置用户ID

    实际用户ID,有效用户ID和设置用户ID 看UNIX相关的书时经常能遇到这几个概念,但一直没有好好去理清这几个概念,以致对这几个概念一直一知半解.今天好好区分了一下这几个概念并总结如下.说白了这几个U ...

  2. sehll 小脚本的应用

    1.模拟linnux登录shell #/bin/bash echo -n "login:" read name echo -n "password:" read ...

  3. Linux配置SSH端口以及密钥登录

    改端口后重启: vim /etc/ssh/sshd_config systemctl restart sshd

  4. Python循环列表删除元素问题

    有人会遇到这种问题,遍历列表,想删除列表中的某几个元素,执行后发现有些并没有删除到, 比如以下代码 a=[1,2,3,4,5,6]print(a) for i in a: if i==3 or i== ...

  5. htt p第一章概述

    http的概述 1 web客户端与服务器是如何通信 2 web资源来自的何方 3 web事务是怎样的工作的 4 http通信所使用的报文结构 5 底层tcp的传输的结构 6不同的http协议体 什么是 ...

  6. MultipleOutputs新旧api

    package MRNB_V4; import java.io.IOException; import java.util.Iterator; import org.apache.hadoop.con ...

  7. mongoDB学习手记2--建库、删库、插入、更新

    上一篇  讲了在windows系统下的安装和启动,本文主要讲怎么建库.删库.插入.更新 在讲之前我们说一下mongoDB的一些基本概念,我们对比关系型数据库能更直观的理解 SQL术语/概念 Mongo ...

  8. HiWord()

    #define HIWORD(I) ( ( WORD ) ( ( ( DWORD )( I ) >> 16) & 0xFFFF ) ). 这个宏传回一个WORD值(16位的无符号整 ...

  9. 变量的声明和定义以及extern的用法

    变量的声明和定义以及extern的用法                                          变量的声明不同于变量的定义,这一点往往容易让人混淆. l         变量 ...

  10. JSP入门 分页

            <div> <%      Integer pageNo = (Integer) request.getAttribute("pageNo");  ...