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] 96. 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 / / \ \ 2 1 2 3 题目大意:给定...
for(intk=left;k<=right;k++) { vector<TreeNode*>lTrees=generateTrees(left,k-1); vector<TreeNode*>rTrees=generateTrees(k+1,right); for(inti=0;i<lTrees.size();i++) { for(intj=0;j<rTrees.size();j++) { TreeNode*idx=newTreeNode(k); idx->left=lTrees[i]; idx->right=rTrees[...
题目链接:https://leetcode.com/problems/unique-binary-search-trees-ii/题目: BST's 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时,递归遍历,根结点大...
125 -- 1:40 App LeetCode 每日一题 Daily Challenge 704 Binary Search 372 -- 3:35 App LeetCode 每日一题 Daily Challenge 1305 All Elements in Two Binary Search Trees 188 1 1:34 App LeetCode 每日一题 Daily Challenge 669 Trim a Binary Search Tree 189 -- 4:44 App LeetCode 每日一题 ...
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 想看英文原文的戳这里。
1038 Binary Search Tree to Greater Sum Tree 从二叉搜索树到更大和树 Description: Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in...
【leetcode】95. Unique Binary Search Trees II 含有N个节点的中序遍历有序的二叉树集 1. 题目 Given an integer 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....
* 题目: 98.Validate Binary Search Tree * 来源:https://oj.leetcode.com/problems/validate-binary-search-tree/ * 结果:AC * 来源:LeetCode * 总结: ***/ #include <iostream> #include <climits> using namespace std; struct TreeNode { int val...