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

  作为一个非科班出身的程序员,还是很有必要刷刷题的。之前有个大牛说,菜鸟刷题可以从AC率高的刷起,这样可以快速培养信心。将LeetCode的题目按照AC率从高到低排序,第一道题目为 【Maximum Depth of Binary Tree】。

  从题目就可以看出,是求二叉树的最大深度。深度的概念,是指从根节点到叶子节点的这条路径上的总节点数。

  要求二叉树的最大深度,只需要求出左子树的深度和右子树的深度,取两者最大值再加 1 ,即为二叉树的最大深度。

  若该二叉树为空,返回 0 。

  然后就是将算法翻译成代码,很简单就有下面的答案:

1 if (root == NULL)
2     return 0;
3 int leftDepth = maxDepth(root->left);
4 int rightDepth = maxDepth(root->right);
5 int max = leftDepth > rightDepth ? leftDepth : rightDepth;
6 return max + 1;

  我刷LeetCode另外还有个目的是为了顺便练习一下C++,所以用 STL 可以简化最后的代码:

1 if (root == NULL)
2     return 0;
3 return std::max(maxDepth(root->left), maxDepth(root->right)) + 1;

  这样看起来简洁多了,所以【Maximum Depth of Binary Tree】这道题完整的答案就是这样:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int maxDepth(TreeNode* root) {
13         if (root == NULL)
14             return 0;
15         return std::max(maxDepth(root->left), maxDepth(root->right)) + 1;
16     }
17 };

PS:《C++ Primer》里面有提到,在C++ 11里面,更推荐用 nullptr 来代替 NULL ,不过这里的原始数据结构就是 NULL , 所以用 NULL 了。

时间: 2024-08-26 02:54:10

我也来刷LeetCode——0、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 树 Maximum Depth of Binary Tree

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Maximum Depth of Binary Tree Total Accepted: 16605 Total Submissions: 38287 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the ro

[LeetCode][Java]Maximum Depth of Binary Tree

https://leetcode.com/problems/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 1

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

[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

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] 8. 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. 然后就直接拿之前的代码了: /** * Definition for binary tree *

[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之Maximum Depth of Binary Tree 以及Balanced 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