【树】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

思路:

找到一个数作为根结点,剩余的数分别划入左子树或者右子树。需设置一个变量来记录左右子树能够生成的所有数。

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {number} n
 * @return {TreeNode[]}
 */
var generateTrees = function(n) {
    if(n==0){
        return [];
    }
    return createTrees(1,n);
};
function createTrees(start,end){
    var results=[];
    if(start>end){
        results.push(null);
        return results;
    }

    for(var i=start;i<=end;i++){
        var left=createTrees(start,i-1);
        var right=createTrees(i+1,end);
        for(var j=0;j<left.length;j++){
            for(var k=0;k<right.length;k++){
                var root=new TreeNode(i);
                root.left=left[j];
                root.right=right[k];
                results.push(root);
            }
        }
    }

    return results;
}
时间: 2024-08-12 00:56:07

【树】Unique Binary Search Trees II的相关文章

Leetcode 树 Unique Binary Search Trees

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Binary Search Trees Total Accepted: 13478 Total Submissions: 37858 Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there

Unique Binary Search Trees II leetcode java

题目: 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 题解:这道题比1难的就是不是返回个数,而

LeetCode: Unique Binary Search Trees II [096]

[题目] 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 confused what "{1,#

LeetCode: 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. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1         3     3      2      1 \      

【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 题解:递归的枚举1~n的每个节点为根节点,然后递归

[LeetCode][JavaScript]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. 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

[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 class Solution { private

[leetcode]Unique Binary Search Trees II @ Python

原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 题意:接上一题,这题要求返回的是所有符合条件的二叉查找树,而上一题要求的是符合条件的二叉查找树的棵数,我们上一题提过,求个数一般思路是动态规划,而枚举的话,我们就考虑dfs了.dfs(start, end)函数返回以start,start+1,...,end为根的二叉查找树. 代码: # Definition for a binary tree node #

经典中的经典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. 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 / /

96. Unique Binary Search Trees &amp;&amp; 95. Unique Binary Search Trees II &amp;&amp; 241. Different Ways to Add Parentheses

96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 Tree Dynamic Program