数据结构系列——完全二叉树(统计节点总数)
发布时间
阅读量:
阅读量
完全二叉树
1. 普通二叉树的遍历,统计节点个数
public int countNodes(TreeNode root){
if (root == null)
return 0;
return 1 + countNodes(root.left) + countNodes(root.right);
}
2. 满二叉树的节点总数
public int countNodes(TreeNode root){
int h = 0;
while (root != null){
root = root.left;
h++;
}
//节点总数就是2^h - 1
return (int)Math.pow(2,h) - 1;
}
3. 完全二叉树的节点总数
public int countNodes(TreeN
全部评论 (0)
还没有任何评论哟~
