# [leetcode]Count Complete Tree Nodes

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 class Solution {
public int res;
Solution(){
this.res = 0;
}
this.res++;
}
public int getCount(){
return this.res;
}
public int countNodes(TreeNode root) {
if(root == null) return 0;
if(root.left == null && root.right == null)
return getCount();
if(root.left != null){
countNodes(root.left);
}
if(root.right != null)
countNodes(root.right);
return getCount();
}
}


public class Solution {
public int res;
Solution(){
this.res = 0;
}
public int getLeftHeight(TreeNode root){
int height = 0;
while(root.left != null) {
root = root.left;
height ++;
}
return height;
}
public int getRightHeight(TreeNode root){
int height = 0;
while(root.right != null) {
root = root.right;
height ++;
}
return height;
}
public int countNodes(TreeNode root) {
if(root == null) return 0;
int lheight = getLeftHeight(root);
int rheight = getRightHeight(root);
if(lheight == rheight) return (2 << lheight) - 1;
else return countNodes(root.left) + countNodes(root.right) + 1;
}
}

#### 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] Count Complete Tree Nodes

2015-06-06 13:45:35

#### 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 124. Binary Tree Maximum Path Sum（二叉树最大路径和）

2016-05-27 00:27:32

#### LeetCode——Count Complete Tree Nodes

2015-07-09 15:07:44

#### Leetcode Count Complete Tree Nodes

2015-12-08 11:58:24