LeetCode104.二叉树的最大深度
题目
代码
一,递归
/**
* Definition for a binary tree node.
* * public class TreeNode {
* * public int val;
* * public TreeNode left;
* * public TreeNode right;
* * public TreeNode(int x) { val = x; }
* * }
*/
public class Solution
{
public int MaxDepth(TreeNode root)
{
if (root == null)
return 0;
else
{
int left_height = MaxDepth(root.left);
int right_height = MaxDepth(root.right);
return Math.Max(left_height, right_height) + 1;
}
}
}