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

\       /     /      / \      \

3     2     1      1   3      2

/     /       \                 \

2     1         2                 3

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

Hide Tags Tree Dynamic Programming

SOLUTION 1:

使用递归来做。

1. 先定义递归的参数为左边界、右边界,即1到n.

2. 考虑从left, 到right 这n个数字中选取一个作为根,余下的使用递归来构造左右子树。

3. 当无解时,应该返回一个null树,这样构造树的时候,我们会比较方便,不会出现左边解为空,或是右边解为空的情况。

4. 如果说左子树有n种组合,右子树有m种组合,那最终的组合数就是n*m. 把这所有的组合组装起来即可

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; left = null; right = null; }
 8  * }
 9  */
10 public class Solution {
11     public List<TreeNode> generateTrees(int n) {
12         // 0.07
13         return dfs(1, n);
14     }
15
16     public List<TreeNode> dfs(int left, int right) {
17         List<TreeNode> ret = new ArrayList<TreeNode>();
18
19         // The base case;
20         if (left > right) {
21             ret.add(null);
22             return ret;
23         }
24
25         for (int i = left; i <= right; i++) {
26             List<TreeNode> lTree = dfs(left, i - 1);
27             List<TreeNode> rTree = dfs(i + 1, right);
28             for (TreeNode nodeL: lTree) {
29                 for (TreeNode nodeR: rTree) {
30                     TreeNode root = new TreeNode(i);
31                     root.left = nodeL;
32                     root.right = nodeR;
33                     ret.add(root);
34                 }
35             }
36         }
37
38         return ret;
39     }
40 }

CODE:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/72f914daab2fd9e2eb8a63e4643297d78d4fadaa/tree/GenerateTree2.java

时间: 2024-10-14 06:04:48

LeetCode: Unique Binary Search Trees II 解题报告的相关文章

[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 #

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 独一无二的二叉搜索树之二

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,#,2,3}&

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

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 /** 2 * Definition for

[LeetCode] Unique Binary Search Trees II dfs 深度搜索

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,#,2,3}&

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][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