All Possible Full Binary Trees Solutions in C++
Number 894
Difficulty Medium
Acceptance 75.3%
Link LeetCode
Other languages —
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/all-possible-full-binary-trees/description//// Author : liuyubobobo/// Time : 2018-08-26#include <iostream>#include <vector>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// Recursion/// Time Complexity: O(2^n)/// Space Complexity: O(2^n)class Solution {private:TreeNode* root;public:vector<TreeNode*> allPossibleFBT(int N) {if(N % 2 == 0)return {};if(N == 1)return {new TreeNode(0)};vector<TreeNode*> ret;for(int i = 1; i < N; i += 2){vector<TreeNode*> left_vec = allPossibleFBT(i);vector<TreeNode*> right_vec = allPossibleFBT(N - 1 - i);for(TreeNode* left: left_vec)for(TreeNode* right: right_vec){TreeNode* root = new TreeNode(0);root->left = left;root->right = right;ret.push_back(root);}}return ret;}};int main() {Solution().allPossibleFBT(7);return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/all-possible-full-binary-trees/description//// Author : liuyubobobo/// Time : 2018-08-26#include <iostream>#include <vector>#include <unordered_map>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// Memory Search/// Time Complexity: O(n^4), Actually, maybe lower than that:)/// Space Complexity: O(n^4)class Solution {private:TreeNode* root;public:vector<TreeNode*> allPossibleFBT(int N){if(N % 2 == 0)return {};unordered_map<int, vector<TreeNode*>> memo;return allPossibleFBT(N, memo);}vector<TreeNode*> allPossibleFBT(int N, unordered_map<int, vector<TreeNode*>>& memo) {if(memo.find(N) != memo.end())return memo[N];if(N == 1)return memo[1] = {new TreeNode(0)};for(int i = 1; i < N; i += 2){vector<TreeNode*> left_vec = allPossibleFBT(i);vector<TreeNode*> right_vec = allPossibleFBT(N - 1 - i);for(TreeNode* left: left_vec)for(TreeNode* right: right_vec){TreeNode* root = new TreeNode(0);root->left = left;root->right = right;memo[N].push_back(root);}}return memo[N];}};int main() {Solution().allPossibleFBT(7);return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/all-possible-full-binary-trees/description//// Author : liuyubobobo/// Time : 2018-08-25#include <iostream>#include <vector>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// Dynamic Programming/// Time Complexity: O(n^4), Actually, maybe lower than that:)/// Space Complexity: O(n^4)class Solution {private:TreeNode* root;public:vector<TreeNode*> allPossibleFBT(int N) {vector<TreeNode*> res;if(N % 2 == 0)return res;if(N == 1)return {new TreeNode(0)};vector<vector<TreeNode*>> dp(N + 1);dp[1] = {new TreeNode(0)};for(int i = 3; i <= N; i += 2){// total i nodesfor(int j = 1; j <= i - 1; j += 2){// left: j nodes, right: i - 1 - j nodesfor(TreeNode* left: dp[j])for(TreeNode* right: dp[i - 1 - j]) {TreeNode* root = new TreeNode(0);root->left = left;root->right = right;dp[i].push_back(root);}}}return dp[N];}};int main() {return 0;}