【leetcode】Unique Binary Search Trees II

来源:互联网 发布:怎样修改网卡mac地址 编辑:程序博客网 时间:2024/06/05 00:48

题目:

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.
这里写图片描述

confused what “{1,#,2,3}” means? > read more on how binary tree is serialized on OJ.

OJ’s Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where ‘#’ signifies a path terminator where no node exists below.

Here’s an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as “{1,2,3,#,#,4,#,#,5}”.

思路:

因为有前面一个题目Unique Binary Search Trees的铺垫,这个题目的解决思路也就比较清晰。n个节点的所有树形结构的个数 f(n) = ∑ f(i) * f(n - 1 - i) ; 这样就可以先将所有树形构造出来。
这个题目还有一个特殊之处就在于要返回的都是BST。所以需要对构造出的所有树形进行编号。对树进行中序遍历就可对BST上每个节点进行编号。
这里需要强调一下,这个题目是用到了动态规划的思想,当前问题需要子问题的解。而我在构造树形的时候,是直接用了子树,这就导致了后面对树的编号也对子树进行了改变。这就是一种浅拷贝,直接将引用赋值,导致了总是wrong answer。应该改为深拷贝,每次调用之前结果的时候,都要申请出空间来复制出以前的结果。

代码

贴上我丑陋的代码。

/** * 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) {        List<ArrayList<TreeNode>> resultList = new ArrayList<ArrayList<TreeNode>>();        //空节点的情况        TreeNode zero = null;        ArrayList<TreeNode> zeroList = new ArrayList<>();        zeroList.add(zero);        resultList.add(zeroList);        //一个节点的情况        TreeNode one = new TreeNode(1);        ArrayList<TreeNode> oneList = new ArrayList<>();//         start = 1;//        generateBST(one);       oneList.add(copyBST(one));        if(n == 1)            return oneList;        resultList.add(oneList);        //两个节点的情况        ArrayList<TreeNode> twoList = new ArrayList<>();        TreeNode two = new TreeNode(1);        //two.right = new TreeNode(2);         two.right = oneList.get(0);        generateBST(two,1);        twoList.add(copyBST(two));        two = new TreeNode(2);        two.left = oneList.get(0);        generateBST(two,1);        twoList.add(copyBST(two));        resultList.add(twoList);            //迭代多个节点的情况        for(int i = 3; i<= n; i++){            ArrayList<TreeNode> numList = new ArrayList<>();            for(int j = 0; j < i; j++){                ArrayList<TreeNode> leftList = resultList.get(j);                ArrayList<TreeNode> rightList = resultList.get(i-1-j);                for(TreeNode left:leftList){                    for(TreeNode right:rightList){                        TreeNode top = new TreeNode(i);                        top.left = copyBST(left);                        top.right = copyBST(right);                        generateBST(top,1);                        numList.add(top);                    }                }            }            resultList.add(numList);        }        return resultList.get(n);    }    public TreeNode copyBST(TreeNode top){        if(top == null)            return null;        TreeNode newTop = new TreeNode(top.val);        newTop.left = copyBST(top.left);        newTop.right = copyBST(top.right);        return newTop;    }    public int generateBST(TreeNode top,int start){        if(top == null)            return start;        start = generateBST(top.left,start);        top.val = start;        start++;        start = generateBST(top.right,start);        return start;    }}
0 0
原创粉丝点击