# 222. Count Complete Tree Nodes

[Link](https://leetcode.com/problems/count-complete-tree-nodes/)

**Solution**

T: O(logN \*logN)

每次計算counNodes時應至少有一個子樹是full樹，可以用 (2^h-1)算出count

最多只有一邊會需要繼續切分計算 (logN \*logN)

```java
   public int countNodes(TreeNode root) {

        int leftHeight = getLeftHeight(root);
        int rightHeight = getRightHeight(root);
        
        if(leftHeight == rightHeight){
            return (1<<leftHeight) - 1; //full binary tree
        }
        else
            return 1 + countNodes(root.left) + countNodes(root.right);
    }
    public int getLeftHeight(TreeNode node){
        int ans = 0;
        while(node != null){
            node = node.left;
            ans++;
        }
        return ans;
    }
    
    public int getRightHeight(TreeNode node){
        int ans = 0;
        while(node != null){
            node = node.right;
            ans++;
        }
        return ans;
    }
```
