leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0222.cpp (715B)


0 class Solution {
1 public:
2 int countNodes(TreeNode *root) {
3 if (!root) return 0;
5 int height = 0;
6 queue<TreeNode *> q;
7 q.push(root);
8 while (!q.empty()) {
9 TreeNode *root = q.front();
10 q.pop();
11 int rh = 0, lh = 0;
12 for (TreeNode *t = root; t; t = t->left)
13 lh++;
14 for (TreeNode *t = root; t; t = t->right)
15 rh++;
16 if (lh == rh)
17 height += exp2(rh) - 1;
18 else {
19 height++;
20 if (root->left) q.push(root->left);
21 if (root->right) q.push(root->right);
22 }
23 }
24 return height;
25 }
26 };