***Leetcode 894. All Possible Full Binary Trees
Posted Z-Pilgrim
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了***Leetcode 894. All Possible Full Binary Trees相关的知识,希望对你有一定的参考价值。
https://leetcode.com/problems/all-possible-full-binary-trees/description/
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL)
* ;
*/
class Solution
public:
vector<TreeNode*> allPossibleFBT(int N)
mem.clear();
return dfs(N);
vector<TreeNode*> dfs(int n)
if (mem.find(n) != mem.end() ) return mem[n];
vector<TreeNode*> ret;
if (n == 1)
ret.push_back(new TreeNode(0));
mem[n] = ret;
return ret;
for (int i = 1; i < n; i+=2)
vector<TreeNode*> left = dfs(i);
vector<TreeNode*> right = dfs(n-i-1);
for (int j = 0; j < left.size(); j++)
for ( int k = 0; k < right.size(); k++ )
TreeNode* root = new TreeNode(0);
root->left = left[j];
root->right = right[k];
ret.push_back(root);
return ret;
private:
map< int, vector<TreeNode*> > mem;
;
以上是关于***Leetcode 894. All Possible Full Binary Trees的主要内容,如果未能解决你的问题,请参考以下文章
leetcode_894. All Possible Full Binary Trees
leetcode 894. 所有可能的满二叉树(All Possible Full Binary Trees)