From 0f3afcf862cc300d608b220fa4c6f5e49c2a9dfb Mon Sep 17 00:00:00 2001 From: Somasree Majumder <56045049+soma2000-lang@users.noreply.github.com> Date: Fri, 30 Jul 2021 04:51:31 +0530 Subject: [PATCH 1/5] Add files via upload --- .../C++/9_ All Possible Full Binary Trees.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Trees/C++/9_ All Possible Full Binary Trees.cpp diff --git a/Trees/C++/9_ All Possible Full Binary Trees.cpp b/Trees/C++/9_ All Possible Full Binary Trees.cpp new file mode 100644 index 0000000..782bbb1 --- /dev/null +++ b/Trees/C++/9_ All Possible Full Binary Trees.cpp @@ -0,0 +1,40 @@ + +#include +using namespace std; +class TreeNode{ +public: + vector allPossibleFBT(int n) { + + if(n&1 == 0)return {}; //FBT can have only odd number of nodes + if(n == 1)return {new TreeNode()}; + + vectorres; + + for(int i = 1; i < n-1;i+=2){ //incrementation by 2 to avoid redundant recursive call for even number of nodes + + vectorleft = allPossibleFBT(i); + vectorright = allPossibleFBT(n-i-1); //1 is reserved for root node, hence n-i-1 + + for(auto l : left) + for(auto r : right) + TreeNode* root = new TreeNode(), root->left = l, root->right = r, res.push_back(root); + } + + return res; + + } +}; +int main(){ + vector v;//declaring a vector + Solution ob; + v = (ob.allPossibleFBT(7)) ;// accessing member functions + for(TreeNode *t : v){ + tree_level_trav(t);//calling the function + } +} +/*Input: n = 7 +Output: [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]] +Example 2: + +Input: n = 3 +Output: [[0,0,0]]/* \ No newline at end of file From 5c2055e8c782d88bce76148eaea7203d7332e6a0 Mon Sep 17 00:00:00 2001 From: Somasree Majumder <56045049+soma2000-lang@users.noreply.github.com> Date: Fri, 30 Jul 2021 11:47:51 +0530 Subject: [PATCH 2/5] Update 9_ All Possible Full Binary Trees.cpp --- Trees/C++/9_ All Possible Full Binary Trees.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Trees/C++/9_ All Possible Full Binary Trees.cpp b/Trees/C++/9_ All Possible Full Binary Trees.cpp index 782bbb1..29f45b9 100644 --- a/Trees/C++/9_ All Possible Full Binary Trees.cpp +++ b/Trees/C++/9_ All Possible Full Binary Trees.cpp @@ -1,3 +1,5 @@ +Time Complexity: O(2^N) +Space Complexity: O(2^N) #include using namespace std; @@ -37,4 +39,4 @@ Output: [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0, Example 2: Input: n = 3 -Output: [[0,0,0]]/* \ No newline at end of file +Output: [[0,0,0]]/* From c00de72211c0e053d4e3fc1d7bb2d4f784207ecc Mon Sep 17 00:00:00 2001 From: Somasree Majumder <56045049+soma2000-lang@users.noreply.github.com> Date: Wed, 4 Aug 2021 10:53:01 +0530 Subject: [PATCH 3/5] Update 9_ All Possible Full Binary Trees.cpp --- Trees/C++/9_ All Possible Full Binary Trees.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Trees/C++/9_ All Possible Full Binary Trees.cpp b/Trees/C++/9_ All Possible Full Binary Trees.cpp index 29f45b9..62d69a6 100644 --- a/Trees/C++/9_ All Possible Full Binary Trees.cpp +++ b/Trees/C++/9_ All Possible Full Binary Trees.cpp @@ -2,9 +2,9 @@ Time Complexity: O(2^N) Space Complexity: O(2^N) #include +#include using namespace std; -class TreeNode{ -public: + vector allPossibleFBT(int n) { if(n&1 == 0)return {}; //FBT can have only odd number of nodes @@ -28,8 +28,6 @@ class TreeNode{ }; int main(){ vector v;//declaring a vector - Solution ob; - v = (ob.allPossibleFBT(7)) ;// accessing member functions for(TreeNode *t : v){ tree_level_trav(t);//calling the function } From 1eccbbb0b4648d629a0e6130e37a6b1a17a888e1 Mon Sep 17 00:00:00 2001 From: Somasree Majumder <56045049+soma2000-lang@users.noreply.github.com> Date: Thu, 5 Aug 2021 00:56:07 +0530 Subject: [PATCH 4/5] Update 9_ All Possible Full Binary Trees.cpp --- Trees/C++/9_ All Possible Full Binary Trees.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Trees/C++/9_ All Possible Full Binary Trees.cpp b/Trees/C++/9_ All Possible Full Binary Trees.cpp index 62d69a6..29a2dca 100644 --- a/Trees/C++/9_ All Possible Full Binary Trees.cpp +++ b/Trees/C++/9_ All Possible Full Binary Trees.cpp @@ -1,3 +1,10 @@ +//Given an integer n, return a list of all possible full binary trees with n nodes. Each node of each tree in the answer must have Node.val == 0. + +//Each element of the answer is the root node of one possible tree. You may return the final list of trees in any order. + +//A full binary tree is a binary tree where each node has exactly 0 or 2 children./// + + Time Complexity: O(2^N) Space Complexity: O(2^N) From 82bae279599fd14c86442a17f3afa51701b0fd8f Mon Sep 17 00:00:00 2001 From: Somasree Majumder <56045049+soma2000-lang@users.noreply.github.com> Date: Mon, 9 Aug 2021 13:44:08 +0530 Subject: [PATCH 5/5] Update 9_ All Possible Full Binary Trees.cpp --- Trees/C++/9_ All Possible Full Binary Trees.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Trees/C++/9_ All Possible Full Binary Trees.cpp b/Trees/C++/9_ All Possible Full Binary Trees.cpp index 29a2dca..39f3f72 100644 --- a/Trees/C++/9_ All Possible Full Binary Trees.cpp +++ b/Trees/C++/9_ All Possible Full Binary Trees.cpp @@ -44,4 +44,8 @@ Output: [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0, Example 2: Input: n = 3 -Output: [[0,0,0]]/* +Output: [[0,0,0]] + +Input: n = 5 +Output: [[0, 0, 0, null, null, 0, 0, null, null, null, null], +[0, 0, 0, 0, 0, null, null, null, null, null, null]]/*