Unique Binary Search Tree - Leetcode

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

思路:dp。设数组res[i]表示n=i时的二叉搜索树个数。

先考虑最简单的情况,res[0] = 0,res[1] = 1。

当n > 1时,一定会存在这样两类二叉搜索树:root节点为1,以及root节点为n本身的树。当root节点为1时,剩下的(n - 1)个节点值全部大于1,都在右孩子的子树里,因此root节点为1的树的总数由规模为n - 1的右孩子子树决定,即个数等于res[n - 1]。当root节点为n时同理,个数也为res[n - 1]。

现在考虑其余节点为root时的情况,假设i为root节点,则值为1到i - 1的节点都会在左孩子子树中,值为i + 1到n的节点都会在右孩子子树中。

因此可能情况是res[i - 1] * res[n - i]。

综上,我们将所有节点为root时的值累加,即为大小为n的二叉搜索树的个数。

 1 class Solution {
 2 public:
 3     int numTrees(int n) {
 4         vector<int> res(n + 1, 0);
 5         res[1] = 1;
 6         for (int i = 2; i <= n; i++)
 7         {
 8             res[i] = res[i - 1] * 2;
 9             for (int j = 2; j < i; j++)
10                 res[i] += res[j - 1] * res[i - j];
11         }
12         return res[n];
13     }
14 };
时间: 2024-10-18 16:20:01

Unique Binary Search Tree - Leetcode的相关文章

[LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉排序树. 举个栗子,给定 n = 3, 共有 5 个. 1 3 3 2 1 \ / / / \ 3 2 1 1

Unique Binary Search Trees leetcode java

题目: 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 题解:  这道题我自己是没啥思路的,感觉就是一种排列组合的计算,但是又不太会..然后网上查了

Convert Sorted List to Binary Search Tree leetcode java

题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题解: 之前做过一道是从sorted array转换到BinarySearchTree的,方法还是一样二分法.但是构造树的方法不是由顶至下了,是要由低至上的建立. 代码如下: 1     static ListNode h; 2   3     public TreeNo

Convert Sorted Array to Binary Search Tree leetcode java

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解: 先复习下什么是二叉搜索树(引自Wikipedia): 二叉查找树(Binary Search Tree),也称有序二叉树(ordered binary tree),排序二叉树(sorted binary tree),是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树

Recover Binary Search Tree leetcode java

题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 题解: 解决方法是利用中序遍历找顺序不对的两个点

Validate Binary Search Tree leetcode java

题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with

Validate Binary Search Tree Leetcode Python

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

Validate Binary Search Tree——LeetCode

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

Validate Binary Search Tree -- LeetCode

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys