描述 Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1…n. For example, Given n = 3, your program should return all 5 unique BST’s shown below.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
思路 涉及到节点的很多算法题都可以考虑使用递归的方法进行求解,这里是二叉树,所以可以这样考虑,假如给定的n=5,那么,可以分解成这么几种情况:1、左边有0个节点,右边有4个节点;2、左边有1个节点,右边有3个节点;3、左边有2个节点,右边有2个节点;4、左边有3个节点,右边有1个节点;5、左边有4个节点,右边有0个节点.,这样五种情况。问题就变成求这五种情况了。而这五种情况又可以进一步的细分,直到变成左边有0个节点,右边有0个节点的情况。在左右两边都为0个节点的情况下面,只需要返回一个新节点即可下面给出代码。 代码(c#)
public IList<TreeNode> GenerateTrees (int n)
{
if (n == 0 ) return new List<TreeNode>();
IList<TreeNode> list = AddNum(0 , n);
for (int i = 0 ; i < list.Count; i++)
{
int index = 1 ;
SetValue(list[i].right, ref index);
list[i] = list[i].right;
}
return list;
}
public void SetValue (TreeNode treeNode, ref int index)
{
if (treeNode != null )
{
SetValue(treeNode.left, ref index);
treeNode.val = index++;
SetValue(treeNode.right, ref index);
}
}
public IList<TreeNode> AddNum (int leftNum, int rightNum)
{
List<TreeNode> result = new List<TreeNode>();
List<TreeNode> left = new List<TreeNode>();
List<TreeNode> right = new List<TreeNode>();
if (leftNum == 0 && rightNum == 0 )
{
result.Add(new TreeNode(0 ));
return result;
}
if (leftNum > 0 )
{
for (int i = 0 ; i < leftNum; i++)
{
left.AddRange(AddNum(i, leftNum - i - 1 ));
}
}
if (rightNum > 0 )
{
for (int i = 0 ; i < rightNum; i++)
{
right.AddRange(AddNum(i, rightNum - i - 1 ));
}
}
if (right.Count > 0 && left.Count == 0 )
{
foreach (var item in right)
{
TreeNode head = new TreeNode(0 );
head.right = item;
result.Add(head);
}
}
else if (left.Count > 0 && right.Count == 0 )
{
foreach (var item in left)
{
TreeNode head = new TreeNode(0 );
head.left = item;
result.Add(head);
}
}
else
{
for (int i = 0 ; i < left.Count; i++)
{
for (int j = 0 ; j < right.Count; j++)
{
TreeNode head = new TreeNode(0 );
head.left = left[i];
head.right = right[j];
result.Add(head);
}
}
}
return result;
}
代码优化 上面的代码能在leetcode上面ac,但是显然效率比较低。这个时候可以分析效率较低的原因,主要是因为递归,因为递归的执行效率是比较低的,尤其是当n比较大的时候。这个时候可以考虑将递归算法转化成非递归的算法。递归的算法是将大n逐步的化小,直至能够求解。假如从反向考虑也是可以的,首先我们求出n=1的情况,然后在求解n=2的情况,最后求解出给定的n。需要注意的是,非递归的方法需要用一个特殊的内存保存已求解的值。具体代码见下。 非递归的方法代码(c#)
public IList<TreeNode> GenerateTrees (int n)
{
if (n == 0 ) return new List<TreeNode>();
List<TreeNode>[,] arr =new List <TreeNode >[n + 1 , n + 1 ];
arr[0 , 0 ] = new List<TreeNode>();
arr[1 , 0 ] = new List<TreeNode>();
arr[1 , 0 ].Add(new TreeNode(0 ));
arr[0 , 1 ] = arr[1 , 0 ];
arr[0 , 0 ].Add(new TreeNode(0 ));
for (int i = 2 ; i <= n; i++)
{
arr[0 , i] = new List<TreeNode>();
for (int j = 0 ; j < i; j++)
{
List<TreeNode> list = new List<TreeNode>();
if (j != 0 && (i - j - 1 ) != 0 )
{
var left = CopyList( arr[j, 0 ]);
var right = CopyList(arr[0 , i - j - 1 ]);
for (int l = 0 ; l < left.Count; l++)
{
for (int r = 0 ; r < right.Count; r++)
{
var head = new TreeNode(0 );
head.left = left[l];
head.right = right[r];
list.Add(head);
}
}
}
else
{
var result = CopyList(arr[j, i - j - 1 ]);
for (int l = 0 ; l < result.Count; l++)
{
var head = new TreeNode(0 );
if (j == 0 )
{
head.left = result[l];
}
else
{
head.right = result[l];
}
list.Add(head);
}
}
arr[0 , i].AddRange(list);
}
arr[i, 0 ] = arr[0 , i];
}
var last = arr[0 , n];
for (int i = 0 ; i < last.Count; i++)
{
int index = 1 ;
SetValue(last[i], ref index);
}
return last;
}
public List<TreeNode> CopyList (List<TreeNode> list)
{
List<TreeNode> result = new List<TreeNode>();
for (int i = 0 ; i < list.Count; i++)
{
TreeNode node = new TreeNode(0 );
CopyNode(list[i], node);
result.Add(node);
}
return result;
}
public void CopyNode (TreeNode node,TreeNode result)
{
if (node.right != null )
{
result.right = new TreeNode(0 );
CopyNode(node.right, result.right);
}
if (node.left != null )
{
result.left = new TreeNode(0 );
CopyNode(node.left, result.left);
}
}
public void SetValue (TreeNode treeNode, ref int index)
{
if (treeNode != null )
{
SetValue(treeNode.left, ref index);
treeNode.val = index++;
SetValue(treeNode.right, ref index);
}
}