您的位置:首页 > 产品设计 > UI/UE

LeetCode 95. Unique Binary Search Trees II

2018-03-09 15:20 459 查看
描述

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);//左边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;
}//递归结束,左右两边的节点都为0
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)
{//右边情况大于0,左边的节点为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];//左边有i个节点跟右边有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);
}
}


相关链接

LeetCode原题地址:https://leetcode.com/problems/unique-binary-search-trees-ii/description/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 二叉树 c# LeetCode