aboutsummaryrefslogtreecommitdiff
path: root/count_complete_tree_node.cpp
blob: 592911083b63a629c832b917be3bce4d5db43885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "leetcode.h"

class Solution {
public:
  int countNodes(TreeNode *root) {
    if (!root)
      return 0;
    int l = 1, r = 1;
    TreeNode *ptr_r = root, *ptr_l = root;
    while (ptr_l = ptr_l->left)
      l++;
    while (ptr_r = ptr_r->right)
      r++;
    if (l == r)
      return pow(2, l) - 1;
    return 1 + countNodes(root->left) + countNodes(root->right);
  }
};