894. All Possible Full Binary Trees
Posted learning-c
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了894. All Possible Full Binary Trees相关的知识,希望对你有一定的参考价值。
列出所有可能的完全二叉树
/** * 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) { if(N%2==0) return {}; int n=(N+1)/2; vector<TreeNode*> ans; if(n==1){ ans.push_back(new TreeNode(0)); return ans; } for(int i=1;i<=n-1;++i){ vector<TreeNode*> left=allPossibleFBT(2*i-1); vector<TreeNode*> right=allPossibleFBT(2*(n-i)-1); for(auto& l : left){ for(auto& r: right){ TreeNode* root=new TreeNode(0); root->left=l; root->right=r; ans.push_back(root); } } } return ans; } };
以上是关于894. All Possible Full Binary Trees的主要内容,如果未能解决你的问题,请参考以下文章
894. All Possible Full Binary Trees
leetcode_894. All Possible Full Binary Trees
Leetcode 894. All Possible Full Binary Trees