Unique Binary Search Trees

江湖小虾米东方未明 2013-10-29 01:20:04
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



class Solution {
public:
int numTrees(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> v(n+1, 0);
v[0] = 1;
v[1] = 1;
v[2] = 2;
if(n<=2)
return v[n];

for(int i=3; i<=n; i++)
for(int j=1; j<=i; j++)
v[i] += v[j-1]*v[i-j];
return v[n];
}
};
求人解释下这个Dp方程式的意义啊。。看不懂
...全文
80 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 1 楼 HuiyuYang_fish 的回复:
v[n] is total of n unique BST's 1 2 ... i ... n, we regard i as the root of tree, 1 ..2..(i-1) is left sub tree, (i+1) ..n is right sub tree. so, v[n]=sum{v[i-1]*v[n-i]}, i=[1,n], v[0]=v[1]=1, ex: Given n =3, v[3]=sum{v[0]*v[2],v[1]*v[1],v[2]*v[0]} = 5 v[2]=sum{v[0]*v[1],v[1]*v[0]} =2 当我们取出一个数x作为根节点后,那么小于x的数必然会成为左子树,大于x的数必然会成为右子树,也就是说对于BST,只和数的个数有关
谢谢MTK_娃娃鱼的帮助,看了你的解释后豁然开朗.
Hony杨 2013-10-29
  • 打赏
  • 举报
回复
v[n] is total of n unique BST's 1 2 ... i ... n, we regard i as the root of tree, 1 ..2..(i-1) is left sub tree, (i+1) ..n is right sub tree. so, v[n]=sum{v[i-1]*v[n-i]}, i=[1,n], v[0]=v[1]=1, ex: Given n =3, v[3]=sum{v[0]*v[2],v[1]*v[1],v[2]*v[0]} = 5 v[2]=sum{v[0]*v[1],v[1]*v[0]} =2 当我们取出一个数x作为根节点后,那么小于x的数必然会成为左子树,大于x的数必然会成为右子树,也就是说对于BST,只和数的个数有关
仅仅是作为搬运工。 算法精粹——举一反三,抛弃题海战术 本书的目标读者是准备去硅谷找工作的码农,也适用于在国内找工作的码农,以及刚接触ACM算法竞赛的新手。 市场上讲解算法的书已经汗牛充栋,为什么还要写这本书呢?主要原因是我对目前市场上的大部分算法书都不太满意。 本书有如下特色: 背后有强大的AlgoHub支持。 本书的所有题目,都可以在 www.algohub.org(即将上线) 上在线判断代码。这样的一大好处是,读者可以边看书,边实现自己的代码,然后提交到网站上验证自己的想法是否正确。AlgoHub的使命是成为最好的算法学习和交流平台。AlgoHub囊括了 POJ, ZOJ, leetcode, HackerRank 等网站的经典题目(一些质量不高的题目则忽略),且 AlgoHub有非常简单的加题系统,用户不需要写一行代码即可自己添加题目,所以AlgoHub的题库还在飞速增长中。 每道题都有完整的代码。 市场上的大部分书,都会讲思路,但给出的代码都是片段,不是完整可编译的代码。本书每题都有完整的代码,且每个代码经过千锤百炼,保证可读性的前提下尽可能简短,方面读者在面试中能快速写出来。 每道题都有多种解法。 本书的宗旨是,用尽可能少的题目,覆盖尽可能多的算法。本书中的的每道题都有多种解法,每种解法不是简单的小改进,而是完全不同的思路,力求举一反三,让读者触类旁通。 本书支持多种主流编程语言。 目前支持 Java, C++, C#, Python, Ruby, JavaScript, Swift, Scala, Clojure, 将来还会支持更多编程语言。 在线阅读 https://www.gitbook.com/book/soulmachine/algorithm-essentials/ 内容目录 介绍 线性表 数组 Remove Duplicates from Sorted Array Remove Duplicates from Sorted Array II Longest Consecutive Sequence Two Sum 3Sum 3Sum Closest 4Sum Remove Element Move Zeroes Next Permutation Permutation Sequence Valid Sudoku Trapping Rain Water Rotate Image Plus One Climbing Stairs Set Matrix Zeroes Gas Station Candy Majority Element Rotate Array Contains Duplicate Contains Duplicate II Contains Duplicate III Product of Array Except Self Game of Life Increasing Triplet Subsequence 单链表 Reverse Linked List Odd Even Linked List Add Two Numbers Reverse Linked List II Partition List Remove Duplicates from Sorted List Remove Duplicates from Sorted List II Rotate List Remove Nth Node From End of List Swap Nodes in Pairs Reverse Nodes in k-Group Copy List with Random Pointer Linked List Cycle Linked List Cycle II Reorder List LRU Cache Palindrome Linked List 字符串 Valid Palindrome Implement strStr() String to Integer (atoi) Add Binary Longest Palindromic Substring Regular Expression Matching Wildcard Matching Longest Common Prefix Valid Number Integer to Roman Roman to Integer Count and Say Anagrams Valid Anagram Simplify Path Length of Last Word Isomorphic Strings Word Pattern 栈和队列 栈 Min Stack Valid Parentheses L

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧