題目:
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) {
if (!root)
return 0;
int Left = 1;
int Right = 1;
if (root->left)
Left = Left + maxDepth(root->left);
if (root->right)
Right = Right + maxDepth(root->right);
return (Left > Right)? Left:right;
}
};