LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II

  1. 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

  题目分析参考自一博文

  把上例的顺序改一下,就可以看出规律了。
  1                1                      2                       3             3
   \                 \                  /      \                  /              / 
   3                  2               1       3               2             1
   /                    \                                       /                \
  2                      3                                   1                    2

  比如,以1为根的树有几个,完全取决于有二个元素的子树有几种。同理,2为根的子树取决于一个元素的子树有几个。以3为根的情况,则与1相同。

  定义Count[i] 为以[0,i]能产生的Unique Binary Tree的数目,

  如果数组为空,毫无疑问,只有一种BST,即空树,
  Count[0] =1

  如果数组仅有一个元素{1},只有一种BST,单个节点
  Count[1] = 1

  如果数组有两个元素{1,2}, 那么有如下两种可能
  1                       2
    \                    /
      2                1
  Count[2] = Count[0] * Count[1]   (1为根的情况)
                     + Count[1] * Count[0]  (2为根的情况)

  再看一遍三个元素的数组,可以发现BST的取值方式如下:
  Count[3] = Count[0]*Count[2]  (1为根的情况)
                 + Count[1]*Count[1]  (2为根的情况)
                 + Count[2]*Count[0]  (3为根的情况)

  所以,由此观察,可以得出Count的递推公式为
  Count[i] = ∑ Count[0...k] * [ k+1....i]     0<=k<i-1
  问题至此划归为一维动态规划。

  程序如下:

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

  

时间: 2024-11-08 12:31:59

LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II的相关文章

LeetCode之“动态规划”:Minimum Path Sum &amp;&amp; Unique Paths &amp;&amp; Unique Paths II

之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or righ

Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 现在考虑网格中有障碍物.那么从左上角到右下角将会有多少条不同的路径? 网格中的障碍物和空位置分别用 1 和 0 来表示. 说明:m 和 n 的值均不超过 100. 示例 1: 输入: [   [0,0,0],  

[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

[leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

[leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree 链接 leetcode 描述 ??Given two binary trees original and cloned and given a reference to a node target in the original tree. ??The cloned tree is a copy of the original tr

39. Recover Binary Search Tree &amp;&amp; Validate Binary Search Tree

Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ 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 prett

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

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

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刷题笔记】Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: 1 /** 2 * Definition for binary tree 3 * public cla

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