给出一个完全二叉树,求出该树的节点个数。
说明:
完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 个节点。
示例:
输入:
1
/ \
2 3
/ \ /
4 5 6
输出: 6
package com.loo;
public class CountTreeNodes {
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
TreeNode left1 = new TreeNode(2);
TreeNode right1 = new TreeNode(3);
TreeNode left2 = new TreeNode(4);
TreeNode left3 = new TreeNode(5);
TreeNode right2 = new TreeNode(6);
TreeNode right3 = new TreeNode(7);
TreeNode left4 = new TreeNode(8);
TreeNode left5 = new TreeNode(9);
TreeNode left6 = new TreeNode(10);
TreeNode left7 = new TreeNode(11);
TreeNode right4 = new TreeNode(12);
root.left = left1;
root.right = right1;
left1.left = left2;
left1.right = left3;
left2.left = left4;
left2.right = left5;
left3.left = left6;
left3.right = left7;
right1.left = right2;
right1.right = right3;
right2.left = right4;
System.out.println(getCountTreeNodes1(root));
System.out.println(getCountTreeNodes2(root));
System.out.println(getCountTreeNodes3(root));
}
/*
规定根节点位于第 0 层,完全二叉树的最大层数为 h。根据完全二叉树的特性可知,完全二叉树的最左边的节点一定位于最底层,因此从根节点出发,每次访问左子节点,直到遇到叶子节点,该叶子节点即为完全二叉树的最左边的节点,经过的路径长度即为最大层数 h。
当 0≤i<h 时,第 i 层包含 个节点,最底层包含的节点数最少为 1,最多为
。
当最底层包含 1 个节点时,完全二叉树的节点个数是 ,当最底层包含
个节点时,完全二叉树的节点个数是
- 1
因此对于最大层数为 h 的完全二叉树,节点个数一定在 [,
- 1] 的范围内,可以在该范围内通过二分查找的方式得到完全二叉树的节点个数。
具体是,根据节点个数范围的上下界得到当前需要判断的节点个数 k,如果第 k 个节点存在,则节点个数一定大于或等于 k,如果第 k 个节点不存在,则节点个数一定小于 k,由此可以将查找的范围缩小一半,直到得到节点个数。如果第 k 个节点位于第 h 层,则 k 的二进制表示包含 h+1 位,其中最高位是 1,其余各位从高到低表示从根节点到第 k 个节点的路径,0 表示移动到左子节点,1 表示移动到右子节点。通过位运算得到第 k 个节点对应的路径,判断该路径对应的节点是否存在,即可判断第 k 个节点是否存在。
*/
public static int getCountTreeNodes1(TreeNode root) {
if (root == null) {
return 0;
}
TreeNode node = root;
int level = 0;
while (node.left != null) {
level++;
node = node.left;
}
int l = 1 << level;
int h = (1 << (level + 1)) - 1;
while (l < h) {
int mid = ((h - l + 1) >> 1) + l; // ((h - l + 1) >> 1) == ((h - l + 1) / 2 )
boolean bool = nodeExists(root , level , mid);
if (bool) {
l = mid;
} else {
h = mid - 1;
}
}
return l;
}
public static boolean nodeExists(TreeNode node , int level , int k) {
TreeNode temp = node;
int bits = 1 << (level - 1);
while (temp != null && bits > 0) {
if ((bits & k) == 0) {
temp = temp.left;
} else {
temp = temp.right;
}
bits >>= 1;
}
return temp!=null;
}
/*
leftCount == rightCount。这说明,左子树一定是满二叉树,因为节点已经填充到右子树了,左子树必定已经填满了。所以左子树的节点总数我们可以直接得到,是 2^leftCount - 1,加上当前这个 root 节点,则正好是 2^leftCount。再对右子树进行递归统计。
leftCount != rightCount。说明此时最后一层不满,但倒数第二层已经满了,可以直接得到右子树的节点个数。同理,右子树节点加 root 节点,总数为 2^rightCount。再对左子树进行递归查找。
*/
public static int getCountTreeNodes2(TreeNode root) {
if (root == null) {
return 0;
}
int leftCount = countLevel(root.left);
int rightCount = countLevel(root.right);
if (leftCount == rightCount) {
return getCountTreeNodes2(root.right) + (1 << leftCount);
}
return getCountTreeNodes2(root.left) + (1 << rightCount);
}
public static int countLevel(TreeNode root) {
int level = 0;
while (root!=null) {
level++;
root = root.left;
}
return level;
}
public static int getCountTreeNodes3(TreeNode root) {
// 如果树是空的,直接返回
if (root == null) {
return 0;
}
// 计算树的高度
int height = getTreeNodeHeight(root);
// 如果树高度是1,直接返回
if (height <= 1) {
return height;
}
// 如果右子树的高度是树的高度减1,说明左子树是满二叉树. 左子树可以通过公式计算,只需要递归右子树
if (getTreeNodeHeight(root.right) == height - 1) {
return (1 << (height - 1)) + getCountTreeNodes3(root.right);
} else {
// 如果右子树的高度不是树的高度减1,说明右子树是满二叉树,可以通过公式计算右子树,只需要递归左子树
return (1 << (height - 2)) + getCountTreeNodes3(root.left);
}
}
// 计算树的高度
public static int getTreeNodeHeight(TreeNode root) {
return root == null ? 0 : 1 + getTreeNodeHeight(root.left);
}
static class TreeNode {
int value;
TreeNode left;
TreeNode right;
TreeNode(int v) {
value = v;
}
}
}