从1..n枚举root
然后左边的是左子树,右边的是右子树。
递归构造就好。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<TreeNode*> make(int start , int end) { vector<TreeNode*>result; if(start > end) { result.push_back(nullptr); return result; } for(int i = start ; i <= end ; ++i) { auto leftsub = make(start , i - 1); auto rightsub = make(i + 1 , end); for(auto left : leftsub) { for(auto right : rightsub) { TreeNode* root = new TreeNode(i); root->left = left; root->right = right; result.push_back(root); } } } return result; } vector<TreeNode *> generateTrees(int n) { return make(1 , n); } };
by 1957