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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int left,right;
if(root==NULL)
return 0;
return (left=maxDepth(root->left))>(right=maxDepth(root->right))?left+1:right+1;
}
};求二叉树的高
本文介绍了一种求解二叉树最大深度的算法实现。通过递归方式遍历二叉树的所有节点,并记录从根节点到最远叶节点的最长路径上的节点数量,即为该二叉树的最大深度。
1403

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



