NC13 二叉树的最大深度
示例1
输入:
{1,2}
复制
返回值:
2
复制
示例2
输入:
{1,2,3,4,#,#,5}
复制
返回值:
3
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @return int整型
*/
public int maxDepth (TreeNode root) {
// write code here
if(root == null ){
return 0 ;
}
int leftDepth = maxDepth(root.left) ;
int rightDepth = maxDepth(root.right) ;
return Math.max(leftDepth , rightDepth)+1 ;
}
}