题目:给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
public class Solution {
public int MaxDepth(TreeNode root) {
if (root == null) return 0;
int x1 = 1;
int x2 = 1;
if(root.left != null)
{
x1 += MaxDepth(root.left);
}
if(root.right != null)
{
x2 += MaxDepth(root.right);
}
return x1 > x2 ? x1 : x2;
}
}