diff options
Diffstat (limited to 'other/count_complete_tree_node.cpp')
-rw-r--r-- | other/count_complete_tree_node.cpp | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/other/count_complete_tree_node.cpp b/other/count_complete_tree_node.cpp new file mode 100644 index 0000000..5929110 --- /dev/null +++ b/other/count_complete_tree_node.cpp @@ -0,0 +1,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); + } +}; |