# LeetCode222 Count CompleteTree Nodes(计算完全二叉树的节点数) Java 题解

Given a complete binary tree, count the number of nodes.

Definition of a complete binary tree from Wikipedia:
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.

public static int countNodes(TreeNode root) {
if(root==null)
return 0;
else {
int left=getLeftHeight(root);
int right=getRightHeight(root);
if(left==right)
return (1<<left)-1;
else {
return countNodes(root.right)+countNodes(root.left)+1;
}
}
}

public static int  getRightHeight(TreeNode root) {
int height=0;
while(root!=null)
{
height++;
root=root.left;
}
return height;

}

public static int  getLeftHeight(TreeNode root) {
int height=0;
while(root!=null)
{
height++;
root=root.right;
}
return height;

}

#### Leetcode[222]-Count Complete Tree Nodes

2015-06-11 13:24:18

#### leetcode 222: Count Complete Tree Nodes

2015-06-06 05:14:48

#### LeetCode222——Count Complete Tree Nodes

2015-08-12 23:56:30

#### LeetCode222:Count Complete Tree Nodes

2015-07-29 17:17:00

#### [leetcode] 222. Count Complete Tree Nodes 解题报告

2016-01-12 06:57:17

#### [各种面试题] 完全二叉树节点个数的统计

2013-09-19 23:00:14

#### leetcode222. 完全二叉树的节点个数

2018-05-30 21:59:40

#### leetcode222---Count Complete Tree Nodes(求完全二叉树节点数)

2016-01-22 22:28:26

#### 222.leetcode.Count Complete Tree Nodes(medium)[完全二叉树 节点个数]

2016-08-08 21:36:01

#### 树——统计完全二叉树的节点数目

2016-07-16 10:18:34