[leetcode] Unique Binary Search Trees II

Given 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

https://oj.leetcode.com/problems/unique-binary-search-trees-ii/

 

思路:要求所有的树,只能递归枚举了。

 

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<TreeNode> generateTrees(int n) {
        return generate(1, n);
    }

    private List<TreeNode> generate(int from, int to) {
        List<TreeNode> res = new ArrayList<TreeNode>();
        if (from > to) {
            res.add(null);
            return res;
        }

        List<TreeNode> left = new ArrayList<TreeNode>();
        List<TreeNode> right = new ArrayList<TreeNode>();
        for (int i = from; i <= to; i++) {
            left = generate(from, i - 1);
            right = generate(i + 1, to);

            for (int j = 0; j < left.size(); j++)
                for (int k = 0; k < right.size(); k++) {
                    TreeNode tmp = new TreeNode(i);
                    tmp.left = left.get(j);
                    tmp.right = right.get(k);
                    res.add(tmp);
                }

        }
        return res;

    }

    public static void main(String[] args) {
        System.out.println(new Solution().generateTrees(3).size());
    }

}
View Code

 

第三遍记录:

  递归执行,generate(m,n)返回由m~n节点组成的树的列表,注意做右子树都有null的情况。

  对于一个节点来看,首先先确定顶点,然后顶点的左右子树分别递归生成树集合,然后根据当前顶点,从左右子树中依次挑选一个子树组成新树。

 

注意generate在from>to的情况下返回的是一个包含null的List。 然后整个helper方法也需要返回一个List of TreeNode

 

import java.util.ArrayList;
import java.util.List;

public class Solution {

    public List<TreeNode> generateTrees(int n) {
        return generate(1, n);
    }

    private List<TreeNode> generate(int from, int to) {
        List<TreeNode> res = new ArrayList<TreeNode>();
      //generate empty tree
        if (from > to) {
            res.add(null);
            return res;
        }
    
        List<TreeNode> left = null, right = null;
       /enumerate root: from 'from' to 'to'
        for (int i = from; i <= to; i++) {
            left = generate(from, i - 1);
            right = generate(i + 1, to);

            for (int j = 0; j < left.size(); j++) {
                for (int k = 0; k < right.size(); k++) {
              
                    TreeNode root = new TreeNode(i);
                    root.left = left.get(j);
                    root.right = right.get(k);
                    res.add(root);
                }
            }

        }
        return res;

    }

    public static void main(String[] args) {
        List<TreeNode> res = new Solution().generateTrees(3);
        System.out.println(res.size());
    }

}

 

 

参考:

http://www.cnblogs.com/cheapcrook/archive/2013/01/29/2880903.html

posted @ 2014-07-02 00:11  jdflyfly  阅读(258)  评论(1编辑  收藏  举报