LeetCode:Unique Binary Search Trees html
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.spa
1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3
分析:依次把每一個節點做爲根節點,左邊節點做爲左子樹,右邊節點做爲右子樹,那麼總的數目等於左子樹數目*右子樹數目,實際只要求出前半部分節點做爲根節點的樹的數目,而後乘以2(奇數個節點還要加上中間節點做爲根的二叉樹數目)code
遞歸代碼:爲了不重複計算子問題,用數組保存已經計算好的結果htm
1 class Solution { 2 public: 3 int numTrees(int n) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 int nums[n+1]; //nums[i]表示i個節點的二叉查找樹的數目 7 memset(nums, 0, sizeof(nums)); 8 return numTreesRecur(n, nums); 9 } 10 int numTreesRecur(int n, int nums[]) 11 { 12 if(nums[n] != 0)return nums[n]; 13 if(n == 0){nums[0] = 1; return 1;} 14 int tmp = (n>>1); 15 for(int i = 1; i <= tmp; i++) 16 { 17 int left,right; 18 if(nums[i-1])left = nums[i-1]; 19 else left = numTreesRecur(i-1, nums); 20 if(nums[n-i])right = nums[n-i]; 21 else right = numTreesRecur(n-i, nums); 22 nums[n] += left*right; 23 } 24 nums[n] <<= 1; 25 if(n % 2 != 0) 26 { 27 int val; 28 if(nums[tmp])val = nums[tmp]; 29 else val = numTreesRecur(tmp, nums); 30 nums[n] += val*val; 31 } 32 return nums[n]; 33 } 34 };
非遞歸代碼:從0個節點的二叉查找樹數目開始自底向上計算,dp方程爲nums[i] = sum(nums[k-1]*nums[i-k]) (k = 1,2,3...i)blog
1 class Solution { 2 public: 3 int numTrees(int n) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 int nums[n+1]; //num[i]表示i個節點的二叉查找樹數目 7 memset(nums, 0, sizeof(nums)); 8 nums[0] = 1; 9 for(int i = 1; i <= n; i++) 10 { 11 int tmp = (i>>1); 12 for(int j = 1; j <= tmp; j++) 13 nums[i] += nums[j-1]*nums[i-j]; 14 nums[i] <<= 1; 15 if(i % 2 != 0) 16 nums[i] += nums[tmp]*nums[tmp]; 17 } 18 return nums[n]; 19 } 20 };
LeetCode:Unique Binary Search Trees II遞歸
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.leetcode
For example,
Given n = 3, your program should return all 5 unique BST's shown below.get
1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3
按照上一題的思路,咱們不單單要保存i個節點對應的BST樹的數目,還要保存全部的BST樹,並且一、二、3和四、五、6雖然對應的BST數目和結構同樣,可是BST樹是不同的,由於節點值不一樣。it
咱們用數組btrees[i][j][]保存節點i, i+1,...j-1,j構成的全部二叉樹,從節點數目爲1的的二叉樹開始自底向上最後求得節點數目爲n的全部二叉樹 本文地址
1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<TreeNode *> generateTrees(int n) { 13 // IMPORTANT: Please reset any member data you declared, as 14 // the same Solution instance will be reused for each test case. 15 vector<vector<vector<TreeNode*> > > btrees(n+2, vector<vector<TreeNode*> >(n+2, vector<TreeNode*>())); 16 for(int i = 1; i <= n+1; i++) 17 btrees[i][i-1].push_back(NULL); //爲了下面處理btrees[i][j]時 i > j的邊界狀況 18 for(int k = 1; k <= n; k++)//k表示節點數目 19 for(int i = 1; i <= n-k+1; i++)//i表示起始節點 20 { 21 for(int rootval = i; rootval <= k+i-1; rootval++) 22 {//求[i,i+1,...i+k-1]序列對應的全部BST樹 23 for(int m = 0; m < btrees[i][rootval-1].size(); m++)//左子樹 24 for(int n = 0; n < btrees[rootval+1][k+i-1].size(); n++)//右子樹 25 { 26 TreeNode *root = new TreeNode(rootval); 27 root->left = btrees[i][rootval-1][m]; 28 root->right = btrees[rootval+1][k+i-1][n]; 29 btrees[i][k+i-1].push_back(root); 30 } 31 } 32 } 33 return btrees[1][n]; 34 } 35 };
【版權聲明】轉載請註明出處:http://www.cnblogs.com/TenosDoIt/p/3448569.html