Unique Binary Search Tree II | leetcode

Source: Internet
Author: User

GivenN, Generate all structurally unique BST'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
The difference between this question and I is that this question needs to output all the situations, and the first question is through dynamic planning, we can clearly express the results of the problem (and finally obtain the answer with the time complexity of O (n) using the qataram number ), if we use the dynamic planning method to obtain a very intuitive expression, we still use the recursive method to select K as the root according to the method in I, recursive left and right Subtrees to obtain all possible combinations of left and right Subtrees.
Java:
/** * 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 List<TreeNode> generateTrees(int n)    {        return generateTrees(1,n);    }        public List<TreeNode> generateTrees(int start,int end)    {        List<TreeNode> result = new ArrayList<TreeNode>();        if (start > end)        {            result.add(null);            return result;        }        for (int i = start; i <= end; i++)        {            List<TreeNode> left =  generateTrees(start, i-1);            List<TreeNode> right = generateTrees(i+1, end);            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);                    result.add(root);                }            }        }        return result;    }}

The code for this question is explained here. The simplest example is the sequence 1, 2, and 3 for your understanding. For the first call, I must call generatetrees () but do not meet the Start> end conditions. To enter the for loop, the next step is to execute generatetrees ), obviously, an empty list is returned, which is recorded as {null}. Therefore, the list named left in generatetrees (1, 3) is {null} (this list. size () is equal to 1 rather than 0). Note that this list only belongs to generatetrees (1, 3). If you confuse this list with other lists, it is hard to understand. In this case, continue to run right, that is, generatetrees (). This is not equal to null. Therefore, you need to execute a recursive function that belongs to generatetrees (). When I is equal to 2, left = {null}, Right = generatetrees (3, 3) = {3}. Here 3 is represented by a special symbol because it is not a value, instead, it is a binary tree node. The value of the node is 3, and the left and right subtree is null. After the result is returned to the upper layer, the new node value is I, that is, 2, and the left subtree is null, the right subtree is the node whose value is 3. In this way, the right subtree is saved. When I is equal to 3, Left = generatetrees (2, 2) = {2}, Right = generatetrees () = {null}. Therefore, both results are returned to generatetrees () and therefore belong to generatetrees) right = {2, 3}, here 2 and 3 are sections Point, and then return to the upper level. 2 and 3 are saved in the right array when the root is 1 (generatetrees (). Therefore, two 1 s are saved in the result array at the top, represent the case where the right subtree is, that is to say, if the root is 1, there are two types, and then the loop at the outermost layer continues to execute, I = 2, 3... n to generate a binary search tree with a root of 2. The final result is the array of the root node of the Binary Search Tree in all cases.

Unique Binary Search Tree II | leetcode

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.