码迷,mamicode.com
首页 > 其他好文 > 详细

Unique Binary Search Tree II

时间:2015-02-07 00:22:44      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

这道题和上一道的思路一样,只是在BST的处理上注意下,包括二叉树的复制,二叉树的调整。

 1 vector<TreeNode* > b;
 2 class Solution {
 3 public:
 4     TreeNode* CopyTree(TreeNode* t)
 5     {
 6         TreeNode* tmp = new TreeNode(t->val);
 7         if(t->left != NULL)
 8             tmp->left = CopyTree(t->left);
 9         if(t->right != NULL)
10             tmp->right = CopyTree(t->right);
11         if(t->left == NULL)
12             tmp->left = NULL;
13         if(t->right == NULL)
14             tmp->right = NULL;
15         //cout<<t->val<<endl;
16         return tmp;
17     }
18 
19     /*void AdjustTree(TreeNode* t)
20     {
21         int tmp = 0;
22         TreeNode* ptr;
23         ptr = t;
24         while(ptr->right != NULL)
25         {
26             tmp = ptr->val;
27             ptr->val = ptr->right->val;
28             ptr->right->val = tmp;
29             ptr = ptr->right;
30         }
31     }*/
32 
33     void AdjustTreeII(TreeNode* t,int val)
34     {
35         if(t->left != NULL)
36             AdjustTreeII(t->left,val);
37         t->val = t->val + val;
38         if(t->right != NULL)
39             AdjustTreeII(t->right,val);
40 
41     }
42 
43     vector<TreeNode *> generateTrees(int n) {
44         vector<TreeNode *> a;
45         vector<vector<TreeNode *> > array;
46        b.push_back(NULL);
47 
48         if(n==0)
49             return b;
50 
51         TreeNode * t = new TreeNode(1);
52         a.push_back(t);
53         array.push_back(a);
54 
55 
56         for(int i=1;i<n;i++)
57         {
58             vector<TreeNode *> v;
59            for(int k=0;k<array[i-1].size();k++)
60            {
61                TreeNode* head1 = new TreeNode(i+1);
62                TreeNode* head2 = new TreeNode(i+1);
63                TreeNode* tmp1 = CopyTree(array[i-1][k]);
64                TreeNode* tmp2 = CopyTree(array[i-1][k]);
65                head1->left = tmp1;
66                head2->right = tmp2;
67                head2->val = 1;
68                AdjustTreeII(head2->right,1);
69               v.push_back(head1);
70               v.push_back(head2);
71 
72               //cout<<tmp1->val<<endl;
73            }
74 
75            for(int j=0;j<=i-2;j++)
76            {
77                for(int k1=0;k1<array[j].size();k1++)
78                     for(int k2=0;k2<array[i-2-j].size();k2++)
79                         {
80                             TreeNode *head = new TreeNode(i+1);
81                             TreeNode* tmp3 = CopyTree(array[j][k1]);
82                             TreeNode* tmp4 = CopyTree(array[i-2-j][k2]);
83                             head -> left = tmp3;
84                             head ->right = tmp4;
85                             head->val = j+2;
86                             AdjustTreeII(head->right,j+2);
87                             v.push_back(head);
88                         }
89            }
90            array.push_back(v);
91         }
92         return array[n-1];
93     }
94 };

 

Unique Binary Search Tree II

标签:

原文地址:http://www.cnblogs.com/ZhangYushuang/p/4278196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!