41. Unique Binary Search Trees && Unique Binary Search Trees II

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
思路: f(n) = Σi=1n  f(n-i)*f(i-1), 其中 f(0) = f(1) = 1; 利用动归记下之前的 f(2)~f(n-1)即可。
class Solution {
public:
    int numTrees(int n) {
        vector<int> f(n+1, 0);
        f[0] = f[1] = 1;
        for(int v = 2; v <= n; ++v)
            for(int pos = 1; pos <= v; ++pos)
                f[v] += f[pos-1] * f[v-pos];
        return f[n];
    }
};

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 为根节点,左右子树根的集合数量相乘,递归,依次得出结果。
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
vector<TreeNode *> generateTreesCore(int start, int end) {
    vector<TreeNode *> vec;
    if(start > end) { vec.push_back(NULL); return vec; }
    for(int cur = start; cur <= end; ++cur) {
        vector<TreeNode *> left = generateTreesCore(start, cur-1);
        vector<TreeNode *> right = generateTreesCore(cur+1, end);
        for(size_t i = 0; i < left.size(); ++i) {
            for(size_t j = 0; j < right.size(); ++j) {
                TreeNode *root = new TreeNode(cur);
                root->left = left[i];
                root->right = right[j];
                vec.push_back(root);
            }
        }
    }
    return vec;
}
class Solution {
public:
    vector<TreeNode *> generateTrees(int n) {
        return generateTreesCore(1, n);
    }
};
           
时间: 2024-10-29 20:12:18

41. Unique Binary Search Trees && Unique Binary Search Trees II的相关文章

PAT Search in a Binary Search Tree

Search in a Binary Search Tree To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison results of the keys. The searching path corresponds to a sequence of keys. For exa

pat04-树7. Search in a Binary Search Tree (25)

04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison res

04-树7. Search in a Binary Search Tree (25)

04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison res

[Algorithms] Refactor a Linear Search into a Binary Search with JavaScript

Binary search is an algorithm that accepts a sorted list and returns a search element from the list. It provides a dramatic performance boost over searching linearly through a list for an element. Let’s play around number of iterations required for e

[Leetcode]700. Search in a Binary Search Tree

700. Search in a Binary Search Tree 本题难度: Easy Topic: Binary Tree Description Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted

Breadth First Search VS Depth First Search (Algorithms)

First lets recall the concept for BFS and DFS. I will use below Binary Tree as an example. Before that, lets go through some of the concepts of Trees and Binary Trees quickly. Concept of Binary Trees A binary tree is made of nodes, where each node co

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

Leetcode | Search in Rotated Sorted Array I &amp; II

Search in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise re

Binary Tree Postorder Traversal &amp;&amp; Binary Tree Preorder Traversal

详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could y

ERROR Shell: Failed to locate the winutils binary in the hadoop binary path

14/12/17 19:18:53 ERROR Shell: Failed to locate the winutils binary in the hadoop binary path java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries. at org.apache.hadoop.util.Shell.getQualifiedBinPath(Shell.jav