一、求二叉树的最大深度
public int maxDepth(Node node){
if(node == null) return 0;
int left = maxDepth(node.left);
int right = maxDepth(node.right);
int height = Math.max(left,right)+1;
return height;
}
二、二叉树的节点数
public int countTree(Node node){
if(node == null) return 0;
int left = countTree(node.left);
int right = countTree(node.right);
int count = left+right+1;
return count;
}
三、二叉树中叶子节点个数
public int numOfleaf(Node node){
if(node == null) return 0;
if(node.left==null && node.right==null) return 1;
int result = numOfleaf(node.left)+numOfleaf(node.right);
return result;
}
四、二叉树第K层节点个数
public int numOfKLevel(Node node,int k){
if(node == null || k<0) return 0;
if(k==1) return 1;
int left = numOfKLevel(node.left,k-1);
int right = numOfKLevel(node.right,k-1);
return left+right;
}