Skip to content

Instantly share code, notes, and snippets.

@ttsugriy
Created January 22, 2019 07:11
Show Gist options
  • Save ttsugriy/cdfa31a4f71508cab769bcb1b2b389fe to your computer and use it in GitHub Desktop.
Save ttsugriy/cdfa31a4f71508cab769bcb1b2b389fe to your computer and use it in GitHub Desktop.
class Solution {
public:
int countNodes(TreeNode* root) {
if (root == nullptr) return 0;
int left_height = 0;
int right_height = 0;
for (auto temp = root; temp != nullptr; temp = temp->left) left_height += 1;
for (auto temp = root; temp != nullptr; temp = temp->right) right_height += 1;
if (left_height == right_height) return pow(2, left_height) - 1;
return 1 + countNodes(root->left) + countNodes(root->right);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment