# Count Complete Tree Nodes

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int countNodes(TreeNode root) {
if (root == null) {
return 0;
}
int left = leftHeight(root);
int right = rightHeight(root);
if (left == right) {
return (2<<left) - 1;
} else {
return countNodes(root.left) + countNodes(root.right) + 1;
}
}
private int leftHeight(TreeNode node) {
TreeNode left = node.left;
int height = 0;
while (left != null) {
left = left.left;
height++;
}
return height;
}
private int rightHeight(TreeNode node) {
TreeNode right = node.right;
int height = 0;
while (right != null) {
right = right.right;
height++;
}
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

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

2017-03-15 09:14:24

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

2015-07-09 20:51:26

#### LeetCode222:Count Complete Tree Nodes

2015-07-29 17:17:00

#### LeetCode222——Count Complete Tree Nodes

2015-08-12 23:56:30

#### 【LeetCode】222. Count Complete Tree Nodes 解题报告（Python）

2018-06-23 10:46:06

#### Count Complete Tree Nodes(****)

2015-08-12 21:58:30

#### Count Complete Tree Nodes （求完全二叉树节点树）

2015-06-27 09:59:22

#### leetcode222. Count Complete Tree Nodes

2017-10-20 16:55:36