package tree;
public class CountleafNode {
/**
* 统计二叉树中叶子节点的个数
* @param args
*/
public static int count(TreeNode root){
if(root==null) return 0;
int sum = 0;
if(root.left==null&&root.right==null){
return 1;
}
sum+=count(root.left);
sum+=count(root.right);
return sum;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
System.out.println(count(root));
}
}
统计二叉树叶子节点的个数
最新推荐文章于 2024-05-23 21:46:38 发布