题目描述:获得一个二叉树的最大深度
思路:递归遍历,设置两个全局变量,count记录当前层数,maxCount记录最大count,之后就是递归的过程了 最后返回最大值
java程序:
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
int count = 0;
int maxCount = Integer.MIN_VALUE;
public int TreeDepth(TreeNode root) {
if(root == null)
return count;
getDepth(root);
return maxCount;
}
public void getDepth(TreeNode root){
if(root==null)
return ;
count++;
if(count>maxCount)
maxCount=count;
getDepth(root.left);
getDepth(root.right);
count--;
}
}
491

被折叠的 条评论
为什么被折叠?



