Leetcode Maximum Depth of Binary Tree
程序员文章站
2024-02-12 22:54:40
...
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 a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL)
return 0;
int depth = 1;
Depth(root,depth,1);
return depth;
}
void Depth(TreeNode* root,int& depth,int count)
{
if(root == NULL)
return;
if(count > depth)
depth = count;
Depth(root->left,depth,count+1);
Depth(root->right,depth,count+1);
}
};
推荐阅读
-
Leetcode Maximum Depth of Binary Tree
-
leetcode【104】Maximum Depth of Binary Tree
-
LeetCode------Maximum Depth of Binary Tree
-
Leetcode 104: Maximum Depth of Binary Tree
-
leetcode Maximum Width of Binary Tree
-
LeetCode-297.Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)
-
【LeetCode】104. Maximum Depth of Binary Tree 二叉树的深度 DFS BFS 递归方式 迭代方式 JAVA
-
leetcode笔记:Invert Binary Tree
-
minimum-depth-of-binary-tree
-
【leetcode】-700. Search in a Binary Search Tree 查找二叉搜索树