Unique Binary Search Trees II

Unique Binary Search Trees II

问题:

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.

思路:

  dfs或者动态规划

我的代码:

public class Solution {
    public List<TreeNode> generateTrees(int n) {
        int[] num = new int[n];
        for(int i = 0; i < n; i++)
        {
            num[i] = i + 1;
        }
        return findNode(num, 0, n);
    }
    public List<TreeNode> findNode(int[] num, int start, int end)
    {
        if(start == end)
        {
            List<TreeNode> list = new ArrayList<TreeNode>();
            list.add(null);
            return list;
        }
        if(start + 1 == end) 
        {
            List<TreeNode> list = new ArrayList<TreeNode>();
            list.add(new TreeNode(num[start]));
            return list;
        }
        List<TreeNode> rst = new ArrayList<TreeNode>();
        for(int i = start; i < end; i++)
        {
           
            List<TreeNode> left = findNode(num, start, i);
            List<TreeNode> right = findNode(num, i + 1, end);
            for(TreeNode l: left)
            {
                for(TreeNode r: right)
                {
                    TreeNode root = new TreeNode(num[i]);
                    root.left = l;
                    root.right = r;
                    rst.add(root);
                }
            }
        }
        return rst;
    }
}
View Code

他人代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
public List<TreeNode> generateTrees(int n) {
    Map<Integer, List<TreeNode>> lists = new HashMap<Integer, List<TreeNode>>();

    List<TreeNode> list = new LinkedList<TreeNode>();
    list.add(null);
    if (n==0) return list;
    lists.put(0, list);

    list = new LinkedList<TreeNode>();
    TreeNode root = new TreeNode(1);
    list.add(root);
    lists.put(1, list);

    for (int i=2; i<=n; i++) {
        list = new LinkedList<TreeNode>();
        for (int j=1; j<=i; j++) {
            for (TreeNode left:lists.get(j-1)) {
                for (TreeNode right:lists.get(i-j)) {
                    root = new TreeNode(j);
                    root.left = left;
                    root.right = greaterCopy(right, j);
                    list.add(root);
                }
            }
        }
        lists.put(i, list);
    }
    return list;
}

private TreeNode greaterCopy(TreeNode node, int add) {
    if (node == null) return null;
    TreeNode copy = new TreeNode(node.val + add);
    copy.left = greaterCopy(node.left, add);
    copy.right = greaterCopy(node.right, add);
    return copy;
}
}
View Code

学习之处:

  • DFS可以解决的问题往往用动态规划也可以解决,对于考虑Size的动态规划问题,往往是两层循环
for(int size=1; size<=n; size++)
{
      for(int j=0; j<=size; j++)
      {
       }   
}    
  • 需要每一次重新 新建一个TreeNode root = new TreeNode(num[i])因为是地址啊,地址啊!这一点一定要注意。

posted on 2015-03-15 19:37  zhouzhou0615  阅读(210)  评论(0编辑  收藏  举报

导航