用C#计算二叉树的最大深度
力扣题目:
解题思路:
如果二叉树为空,则返回0;如果不为空,则计算二叉树的最深长度,返回。
代码实现:
/**
* 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
return Math.Max(MaxDepth(root.left), MaxDepth(root.right))+1;
}
}