int numTrees(int n) { if (n < 0) return 0; vector<int> trees(n+1, 0); trees[0] = 1; for(int i = 1; i <= n; i++) for (int j = 0; j < i; j++) trees[i] += trees[j] * trees[i-j-1]; return trees[n]; } };
LeetCode: Unique Binary Search Trees I & II Title: Givenn, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Givenn= 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 首先...
LeetCode: 96. Unique Binary Search Trees 题目描述 Given n, how many structurally unique BST’s (binary search trees) that store values 1 … n? Example: Input: 3 Output: 5 Explanation: Given n = 3, there are a total of 5 unique BST's: 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3...
LeetCode Unique Binary Search Trees 1.题目 Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n? For example, Givenn= 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. 2. 3. 4....
Postorder(左子树、右子树、根, Depth-first Search) Level order(Breath-first Search) leetcode: 144, 94, 145 Tree structNode{Node*left;Node*right;intdata;};Node*root=...;// 假設已經建立二元樹了 Preorder // preorder traversalvoidtraversal(Node*p){if(!p)return;cout<<p->data;// 先輸出樹...
@(LeetCode) 问题描述 给定一个整数n,能构造出多少种BST,使其节点值包括1~n? 栗子: 输入:3 输出:5 解释: 可构造出 5 种 BST,如下所示: 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3 想看英文原文的戳这里。
95. Unique Binary Search Trees II Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n. Example: Input:3Output:[ [1,null,3,2],[3,2,null,1],[3,1,null,null,2], [2,1,3],[1,null,2,null,3]]Explanation:The above output ...
Binary Search Tree Iterator 74 -- 1:31 App Leetcode-0081. Search in Rotated Sorted Array II 84 -- 2:53 App Leetcode-0144. Binary Tree Preorder Traversal 63 -- 4:35 App Leetcode-0101. Symmetric Tree 7 -- 1:29 App Leetcode-0167. Two Sum II - Input Array Is Sorted 12...
Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n? For example, Givenn= 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 Solution: ...
Unique Binary Search Trees Givenn, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 ...