标签:nbsp 返回 遍历 des 应该 back solution code 情况
1 class Solution 2 { 3 public: 4 vector<TreeNode *> generateTrees(int n) 5 { 6 if (n) return generate(1, n); 7 else return vector<TreeNode *>{}; 8 } 9 10 vector<TreeNode *> generate(int left, int right) 11 { 12 vector<TreeNode *> ans; 13 if (left > right) 14 { 15 ans.push_back(NULL); 16 return ans; 17 } 18 for (int i = left; i <= right; i++) 19 { 20 vector<TreeNode *> left_nodes = generate(left, i - 1); 21 vector<TreeNode *> right_nodes = generate(i + 1, right); 22 for (TreeNode *left_node : left_nodes) 23 { 24 for (TreeNode *right_node : right_nodes) 25 { 26 TreeNode *t = new TreeNode(i); 27 t->left = left_node; 28 t->right = right_node; 29 ans.push_back(t); 30 } 31 } 32 } 33 return ans; 34 } 35 };
标签:nbsp 返回 遍历 des 应该 back solution code 情况
原文地址:https://www.cnblogs.com/yuhong1103/p/12609723.html