学习笔记:LeetCode104:Maximum Depth of Binary Tree

LeetCode104: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 binary tree
3  * public class TreeNode {
4  *     int val;
5  *     TreeNode left;
6  *     TreeNode right;
7  *     TreeNode(int x) { val = x; }
8  * }
9  */

基于问题,考虑使用分治递归的思想,将问题转化:当前节点左子树与右子树的最大深度+1 = 当前树的最大深度

逐层下推直到不再有子节点为止,于是能得出递归返回条件:

if(root == null){
            return 0;
        }

递归策略:当前节点左子树和当前节点右子树

最终结果:当前树的最大深度 = 当前节点左子树与右子树的最大深度+1

完整代码如下:

 1 public class Solution {
 2     public int maxDepth(TreeNode root) {
 3         if(root == null){
 4             return 0;
 5         }
 6         int left = maxDepth(root.left);
 7         int right = maxDepth(root.right);
 8         return 1+Math.max(left,right);
 9     }
10 }
时间: 2024-11-10 10:30:48

学习笔记:LeetCode104:Maximum Depth of Binary Tree的相关文章

【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. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: 1 /** 2 * Definition for binary tree 3 * public cla

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. 二. 题目分析 这道题和Minimum Depth of Binary Tree一题相似,这个是求最大深度的,就是对二叉树进行递归,然后将子树的最大深度进行返回,最

LeetCode104: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. Show Tags Show Similar Problems 计算二叉树的深度,使用递归求解非常容易,一个节点的深度等于它的左子树的深度和它的右子树的深度的最大值加上1,空树的深

LeetCode104——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. 难度系数: 容易 实现 int maxDepth(TreeNode *root) { if (root == NULL) return 0; if (root->left == N

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

34: Maximum Depth of Binary Tree

/************************************************************************/        /*       34:     Maximum Depth of Binary Tree                                        */        /************************************************************************

【LeetCode】 Maximum Depth of Binary Tree

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. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root

leetcode -day24 Maximum Depth of Binary Tree & Binary Tree Zigzag Level Order Traversal

1.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. class Solution { public: int maxDepth(TreeNode *root) { inM

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