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.

 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
11 public class Solution {
12     public int maxDepth(TreeNode root) {
13         return (root == null)?0:Math.max(1+maxDepth(root.left),1+maxDepth(root.right));
14     }
15 }
时间: 2024-08-02 00:16:46

LeetCode 104的相关文章

[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

图解精选 TOP 面试题 002 | LeetCode 104. 二叉树的最大深度

该系列题目取自 LeetCode 精选 TOP 面试题列表:https://leetcode-cn.com/problemset/top/ 题目描述 原题链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明:叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / 9 20

[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. 递归遍历 左子树 和 右子树 一刷: public int maxDepth(TreeNode root) { if(root == null){ return 0; } int

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

题目: 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和当前的最大

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. 思路:很基础的一个题,DFS即可.代码如下: /** * Definition for a binary tree node. * public class TreeNode {

[LeetCode#104, 111]Maximum Depth of Binary Tree, Minimum Depth of Binary Tree

The problem 1: 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. My analysis: The recursion solution for this problem is very elegant!It inc

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. 题目标签:Tree 这道题目给了我们一个二叉树,要我们找到最大深度,就是从root点到最深的那个点之间点的数量.利用post order 来遍历二叉树,对于每一个点,它的两个chi

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. 分析:求二叉树的最大深度 解法一:很容易想到的便是递归(深度优先搜索) (1)如果根节点是空,则返回0:否则转到(2) (2)  l = 左子树的最大深度; r = 右子树的最大深