`
frank-liu
  • 浏览: 1687648 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

leetcode: 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.

原问题链接:https://leetcode.com/problems/maximum-depth-of-binary-tree/

 

问题分析

  这个问题的思路比较容易得到。要求树的最大深度,对任意的一个节点来说,主要递归的求它两个子树的最大深度再加1。而如果当前节点为空,则返回0。

  详细的代码实现如下:

 

/**
 * 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;
        return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }
}

 

分享到:
评论

相关推荐

    java-leetcode-maximum-depth-of-binary-tree

    java java_leetcode-maximum-depth-of-binary-tree

    python-leetcode题解之104-Maximum-Depth-of-Binary-Tree

    python python_leetcode题解之104_Maximum_Depth_of_Binary_Tree

    js-leetcode题解之104-maximum-depth-of-binary-tree.js

    js js_leetcode题解之104-maximum-depth-of-binary-tree.js

    leetcode答案-leetcode:leetcode

    leetcode 答案 leetcode 08/18 Unique Paths 应该是简单的数学排列组合问题,提炼一下其实就一句话:有m个黑球,n个白球,有多少种不同的排列方式。 我数学太差,没找到答案,直接上了动态规划。 Unique Paths II ...

    leetcode:LeetCode解决方案

    "二叉树的最大路径和"(Maximum Depth of Binary Tree)要求找到从根节点到任意叶子节点的最大路径。 5. **哈希表**:哈希表提供了快速的查找和插入操作,常用于解决集合问题。"哈希表实现LRU缓存策略"(LRU Cache...

    LeetCode和剑指offer中的算法题的题目和解法 和 常见算法汇总

    1. Math Implementation Questions(数学实现题) ...5.1 Maximum Depth of Binary Tree(二叉树的深度) 5.2 Invert Binary Tree(反转二叉树) 5.3... 5.4... 5.5... 6. String Questions(字符串相关问题)

    LeetCode:LeetCode刷题

    例如,"二叉树的最大路径和"(Maximum Depth of Binary Tree)问题,计算二叉树的最大路径和。 5. **回溯(Backtracking)**:回溯是一种解决搜索问题的方法,常用于组合优化问题。例如,"N皇后"(N-Queens)问题,...

    2sumleetcode-LeetCode:力码

    2sum leetcode 轮廓 1_count_and_say.cpp - super_ugly_number.cpp - Detect_Pattern.cpp - degree_of_array.cpp ...2Sum_Data_Structure_Design.cpp ...Remove_Nth_Node_From_End_of_List.cpp ...Maximum_Depth_B

    Leetcode 使用 Javascript 的解决方案.zip

    Javascript 的解决方案Leetcode Problems and interview problems in Javascript10 Regular Expresion Matching.js100 Same Tree.js101 Symmetric Tree.js102 Binary Tree Level Order Traversal.js103 Binary Tree ...

    leetcode:我的leetcode培训。 实践使完美!

    "二叉树的最大路径和"(Maximum Depth of Binary Tree)是其中的一例。 5. **堆栈和队列**:如实现栈或队列的功能,或者使用它们解决实际问题。"有效括号"(Valid Parentheses)要求使用栈来检查括号匹配性。 6. *...

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...

    leetcode答案-leetcode-java:leetcode的Java代码

    leetcode 答案leetcode-java leetcode.com 的 Java 答案 ================索引================ com.leetcode.array Search a 2D Matrix Spiral Matrix com.leetcode.list Linked List Cycle Linked List Cycle II ...

    Leetcode book刷题必备

    26. Maximum Depth of Binary Tree:计算二叉树的最大深度。 27. Minimum Depth of Binary Tree:计算二叉树的最小深度。 28. Balanced Binary Tree:判断一个二叉树是否是平衡二叉树。 29. Convert Sorted Array to...

    leetcode-js:算法和数据结构是一个程序员的灵魂,LeetCode JavaScript TypeScript 题解

    leetcode-js Leecode 经典题目 JavaScript TypeScript 题解。 Leetcode's answers by ...104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 (Pascal's Triangle) 119.杨辉三角 II (Pascal's Triangle)

    四平方和定理leetcode-leetcode-practice:个人LeetCode练习代码

    104.maximum-depth-of-binary-tree (二叉树的最大深度) 105.construct-binary-tree-from-preorder-and-inorder-traversal (从前序与中序遍历序列构造二叉树) 106.construct-binary-tree-from-inorder-and-postorder-...

    javalruleetcode-what_the_dead_men_say:what_the_dead_men_say

    java lru leetcode what_the_dead_men_say 所以这只是一个 repo,我从leetcode.com存储我的问题解决方案。 二叉树 0098 Validate Binary ...Tree ...Binary ...Maximum depth of binary tree - Java Iterative

    leetcode答案-LeetCode-Trip:LeetCode刷题代码,大佬勿入

    leetcode 答案 LeetCode-Trip LeetCode刷题代码,大佬勿入。 为一年后的研究生找工作准备 目标是BAT的算法岗哈哈哈哈哈 争取做到每日一更 嗯…… 19.10.22:鸽了这么久,我又回来了……主要在实验室天天没啥事,过于...

    Leetcode题目+解析+思路+答案.pdf

    - **Depth of Binary Tree**:计算二叉树的深度。 - **Construct Binary Tree**:根据给定的数组构建二叉树。 - **Binary Tree Level Order Traversal**:二叉树的层次遍历。 - **Symmetric Tree**:判断一个...

    LeetCode最全代码

    318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-...

    LeetCode去除数组重复元素-Arithmetic-Swift:一些算法的swift实现

    LeetCode去除数组重复元素 Arithmetic-Swift 一些算法的swift实现 桶排序 冒泡排序 快速排序 ##正好看见LeetCode可以刷Swift的题目 开始慢慢刷 swift有playground 做起来还是相当方便的 已完成题目 ----2016.9.30 两...

Global site tag (gtag.js) - Google Analytics