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 == NULL && root->right == NULL) {
        return 1;
    }
    int maxl = maxDepth(root->left);
    int maxr = maxDepth(root->right);
    return maxl > maxr ? maxl + 1 : maxr + 1;
}
时间: 2024-11-10 20:33:02

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

学习笔记: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 * De

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

Maximum Depth of Binary Tree leetcode 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     public int maxDepth(TreeNode root) {2         if(root==null)3         

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. 题解: 题意比较清楚, 找到从root出发最长的一条路径的长度. 采用DFS即可. 相似的一道题: Minimux Depth of Binary Tree 解法: http://