Unique Binary Search Trees II

Source: Internet
Author: User

GivenN, Generate all structurally uniqueBST's(Binary Search Trees) that store values 1...N.

For example,
GivenN= 3, your program shocould return all 5 unique BST's shown below.

   1         3     3      2      1    \       /     /      / \           3     2     1      1   3      2    /     /       \                    2     1         2                 3

Confused what"{1,#,2,3}"Means?


OJ's binary tree serialization:

The serialization of a binary tree follows a level order traversal, where '# 'signiies a path Terminator where no node exists below.

Here's an example:

   1  /  2   3    /   4         5
The above binary tree is serialized "{1,2,3,#,#,4,#,#,5}".

Answer
/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; left = null; right = null; } * } */public class Solution {    public TreeNode copyNode(TreeNode node)    {        if(node==null)        {            return null;        }        TreeNode nodeCopy=new TreeNode(node.val);        List<TreeNode> listOriginal=new LinkedList<TreeNode>();        List<TreeNode> listCopy=new LinkedList<TreeNode>();        listOriginal.add(node);        listCopy.add(nodeCopy);        while(listOriginal.size()>0)        {            TreeNode original=listOriginal.get(0);            TreeNode copy=listCopy.get(0);            if(original.left!=null)            {                copy.left=new TreeNode(original.left.val);                listOriginal.add(original.left);                listCopy.add(copy.left);            }            if(original.right!=null)            {                copy.right=new TreeNode(original.right.val);                listOriginal.add(original.right);                listCopy.add(copy.right);            }            listOriginal.remove(0);            listCopy.remove(0);        }        return nodeCopy;    }    public List<TreeNode> generateTrees(int start,int end)    {        List<TreeNode> result=new LinkedList<TreeNode>();        TreeNode root=null;        if(start>end)        {            result.add(null);            return result;        }        for(int i=start;i<=end;i++)        {                        List<TreeNode> listLeft=generateTrees(start,i-1);            List<TreeNode> listRight=generateTrees(i+1,end);            for(TreeNode left:listLeft)            {                for(TreeNode right:listRight)                {                    root=new TreeNode(i);                    root.left=copyNode(left);                    root.right=copyNode(right);                    result.add(root);                }            }        }        return result;    }    public List<TreeNode> generateTrees(int n) {        if(n<0)        {            return new LinkedList<TreeNode>();        }        return generateTrees(1,n);    }}


Unique Binary Search Trees II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.