统计二叉树叶子节点的个数

来源:互联网 发布:守望先锋各英雄数据 编辑:程序博客网 时间:2024/04/27 19:21

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));}}


0 0