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 fastest leaf node.
/*
struct TreeNode{
int val;
TreeNode *left;
TreeNode *right;
*/
class Solution{
public:
int maximumofDepth(TreeNode *root){
if(root == NULL)
return 0;
int l = maximumofDepth(root->left);
int r = maximumofDepth(root->right);
return (l > r ? l : r) + 1;
}
};
本文介绍了一种求解二叉树最大深度的算法。该算法通过递归方式遍历二叉树的所有节点,找到从根节点到最远叶子节点的路径长度。此算法适用于计算机科学中的数据结构学习及面试题解答。
410

被折叠的 条评论
为什么被折叠?



