Created
January 22, 2019 07:11
-
-
Save ttsugriy/cdfa31a4f71508cab769bcb1b2b389fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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