From 7d5fd6f22587d314dcd4d0347fda19ed5023e3ce Mon Sep 17 00:00:00 2001 From: tamanna863 <75742463+tamanna863@users.noreply.github.com> Date: Thu, 6 Oct 2022 22:16:51 +0530 Subject: [PATCH] Create Count Complete Tree Nodes.cpp --- Count Complete Tree Nodes.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Count Complete Tree Nodes.cpp diff --git a/Count Complete Tree Nodes.cpp b/Count Complete Tree Nodes.cpp new file mode 100644 index 0000000..a053797 --- /dev/null +++ b/Count Complete Tree Nodes.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int findleftheight(TreeNode* root){ + int h=0; + while(root!=NULL){ + h++; + root=root->left; + } + return h; + } + int findrightheight(TreeNode* root){ + int h1=0; + while(root!=NULL){ + h1++; + root=root->right; + } + return h1; + } + int countNodes(TreeNode* root) { + if(root==NULL)return 0; + int lh=findleftheight(root); + int rh=findrightheight(root); + if(lh==rh)return (1 << lh)-1; + return 1+countNodes(root->left) + countNodes(root->right); + + } +};