Share my C# solution, for the new C# coder

• ...
/**

• Definition for a binary tree node.

• public class TreeNode {

• ``````public int val;
``````
• ``````public TreeNode left;
``````
• ``````public TreeNode right;
``````
• ``````public TreeNode(int x) { val = x; }
``````
• }
*/
public class Solution {
public IList<TreeNode> GenerateTrees(int n) {

``````     if (n == 0) return new List<TreeNode>();
IList<IList<TreeNode>> allTrees = new List<IList<TreeNode>>();
TreeNode treenode = new TreeNode(1);
if (n == 1) return allTrees[0];
for (int i = 2; i <= n; i++)
{
IList<TreeNode> ltmp = new List<TreeNode>();
for (int j = 1; j <= i; j++)
{
// if top tree is j.so need to foreach the (j-1)th treeList and (i-j)th treeList ,
//this will generate a*b rootTreeNode. a stands for the num of (j-1)th treeList's treenodes and b stands for the num of (i-j)th treeList's treenodes.
if (j > 1&&j<i)
{
foreach (TreeNode tt1 in allTrees[j - 2])
{
foreach (TreeNode tt2 in allTrees[i-j-1])
{
TreeNode t = new TreeNode(j);
t.left = Copy(tt1, 0);
t.right = Copy(tt2, j);
}
}
}
// if top tree is 1,so just to foreach the (i-1)th treeList,because all the nodes are in the right.
if(j==1)
{
foreach (TreeNode tt1 in allTrees[i-2])
{
TreeNode t1 = new TreeNode(j);
t1.right = Copy(tt1, 1);
}
}
// if top tree is i,so just to foreach the (i-1)th treeList,because all the nodes are in the left.
if (j==i)
{
foreach (TreeNode tt2 in allTrees[i-2])
{
TreeNode t1 = new TreeNode(j);
t1.left = Copy(tt2, 0);
}
}
}
}
return allTrees[n - 1];
``````

}
...
//deep copy
...
...
public TreeNode Copy(TreeNode sour, int n)
{
if (sour == null) return null;
TreeNode des = new TreeNode(sour.val + n);
des.right = Copy(sour.right, n);
des.left = Copy(sour.left, n);
return des;
}
}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.