LeetCode OJ 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.

很简单,直接上代码:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public int maxDepth(TreeNode root) {
12         if(root == null) return 0;
13
14         int leftmax = maxDepth(root.left);
15         int rightmax = maxDepth(root.right);
16
17         return leftmax>rightmax?leftmax+1:rightmax+1;
18     }
19 }
时间: 2024-08-05 19:29:48

LeetCode OJ 104. Maximum Depth of Binary Tree的相关文章

LeetCode OJ - Minimum && Maximum Depth of Binary Tree

这两道题用递归的解法都很简单,只是稍有不同. 下面是AC代码: 1 /** 2 * Given a binary tree, find its minimum depth. 3 * the minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 4 * @param root 5 * @return 6 */ 7 public in

【一天一道LeetCode】#104. Maximum Depth of Binary Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th

【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 longest path from the root node down to the farthest leaf node. Solution: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val;

[LeetCode] NO.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. [题目解析] 这是一道很常见也很简单的题目.直接看代码吧. private class TreeNode { int val; TreeNode left; TreeNo

LeetCode之104. Maximum Depth of Binary Tree

-------------------------------- 递归遍历即可 AC代码: /** * 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

Leetcode刷题记录[python]——104 Maximum Depth of Binary Tree

一.前言 对于这类抽象问题还有self用法,还要多做多练习,此题是参考其他答案所做. 二.题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. # Definitio

[Lintcode]97. Maximum Depth of Binary Tree/[Leetcode]104. Maximum Depth of Binary Tree

97. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree 本题难度: Easy Topic: Binary Tree Description 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 f

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

Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question Solution 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

我也来刷LeetCode——0、Maximum Depth of Binary Tree(开篇)

作为一个非科班出身的程序员,还是很有必要刷刷题的.之前有个大牛说,菜鸟刷题可以从AC率高的刷起,这样可以快速培养信心.将LeetCode的题目按照AC率从高到低排序,第一道题目为 [Maximum Depth of Binary Tree]. 从题目就可以看出,是求二叉树的最大深度.深度的概念,是指从根节点到叶子节点的这条路径上的总节点数. 要求二叉树的最大深度,只需要求出左子树的深度和右子树的深度,取两者最大值再加 1 ,即为二叉树的最大深度. 若该二叉树为空,返回 0 . 然后就是将算法翻译