# Middle-题目95：222. Count Complete Tree Nodes

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

public class Solution {
public int countNodes(TreeNode root) {
if(root == null)
return 0;
else {
int leftHeight = 0,rightHeight = 0;
TreeNode left = root,right = root;
while(left!=null) {
left=left.left;
leftHeight++;
}
while (right!=null) {
right = right.right;
rightHeight++;
}
if(leftHeight == rightHeight) // full binary tree
//return (int) (Math.pow(2, leftHeight)-1);
return (2<<(leftHeight-1)) - 1;
else
return 1+countNodes(root.left)+countNodes(root.right);
}
}
}

112ms,beats 59.26%,众数116ms，3.89%
cmershen的碎碎念：

#### leetcode 222: Count Complete Tree Nodes

2015-06-06 05:14:48

#### LeetCode222:Count Complete Tree Nodes

2015-07-29 17:17:00

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

2015-07-09 20:51:26

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

2015-06-11 13:24:18

#### LeetCode 222. Count Complete Tree Nodes 题解——Java

2017-03-15 09:14:24

#### LeetCode222——Count Complete Tree Nodes

2015-08-12 23:56:30

#### [LeetCode] Count Complete Tree Nodes

2015-06-06 13:45:35

#### 222. Count Complete Tree Nodes

2016-03-22 10:29:26

#### 222. Count Complete Tree Nodes**

2016-12-24 13:23:10

#### 222. Count Complete Tree Nodes(leetcode) 超时

2016-10-03 12:35:13