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

tree

时间:2016-04-15 18:19:53      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

树的一些知识点

uva,536

为一棵n叉树,k为深度

考虑到所以猫的高度:  Ih(1+n/(n+1)+n*n/(n+1)*(n+1)+.....+n^k/(n+1)^k)

不工作的猫:1+n+n^2+....+n^k-Lc

得到。Lc=n^k    Ih=(n+1)^k

求n  log(Ih)/log(n+1)=log(Lc)/log(n);

技术分享
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cmath>
 5 #define eps 1e-10
 6 using namespace std;
 7 
 8 int main()
 9 {
10     int Ih,Lc;
11     int left,right;
12     while(~scanf("%d %d",&Ih,&Lc))
13     {
14         if(!Ih && !Lc)
15             break;
16         int n,k=0;
17         if(Lc==1)  //考虑情况为1,因为对等比数列来说,公比不能为一
18 //否则求前n项和就rte,
19         {
20             left=(ceil)(log(Ih)/log(2));  //int取整+0.5或者ceil
21             right=2*Ih-1;
22         }
23         else
24         {
25         for(n=1;n<=Lc;n++)
26         {
27             if(fabs(log(n)*log(Ih)-log(Lc)*log(n+1))<eps)
28                 break;
29         }
30         k=(int)(log(Ih)/log(n+1)+0.5);
31         left=(1-Lc)/(1-n);  
32         right=(n+1)*Ih-n*Lc;
33         }
34         printf("%d %d\n",left,right);
35     }
36     return 0;
37 }
View Code
技术分享
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cmath>
 5 #define eps 1e-10
 6 using namespace std;
 7 
 8 int main()
 9 {
10     int Ih,Lc;
11     int left,right;
12     while(~scanf("%d %d",&Ih,&Lc))
13     {
14         if(!Ih && !Lc)
15             break;
16         int n=1,k=0;
17         if(Lc==1)
18         {
19             left=(ceil)(log(Ih)/log(2));
20             right=2*Ih-1;
21         }
22         else
23         {
24         while(fabs(log(n)*log(Ih)-log(Lc)*log(n+1))>eps)
25             n++;
26         k=(int)(log(Ih)/log(n+1)+0.5);
27         left=(1-Lc)/(1-n);
28         right=(n+1)*Ih-n*Lc;
29         }
30         printf("%d %d\n",left,right);
31     }
32     return 0;
33 }
其实不用考虑n的范围,不需要额外的空间

 

uva,536

前跟:先访问根再访问左再访问右子树

中跟:先访问左再访问根在访问右子树

后跟:先访问左子树再访问右子树再访问根

通过前根的点在中根中位置确定左右子树,然后递归最右再最左,压栈,压栈的顺序和后根遍历的顺序正好相反。

例如:DBACEGF  ABCDEFG

先对D找到在中根的位置,然后访问右边,然后找到E然后G然后F然后B,C,A,

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <stack>
 5 
 6 using namespace std;
 7 
 8 stack<char> st;
 9 char PD[30],ID[30];
10 
11 void find(int p1,int p2,int q1,int q2)
12 {
13     if(p1>p2) return;
14     st.push(PD[p1]);
15     int root=q1;
16     for(;ID[root]!=PD[p1];++root);
17     find(root+1-q1+p1,p2,root+1,q2);
18     find(p1+1,root-q1+p1,q1,root-1);
19 }
20 
21 int main()
22 {
23     while(cin>>PD>>ID)
24     {
25         int L=strlen(PD)-1;
26         find(0,L,0,L);
27         while(!st.empty())
28         {
29             char c=st.top();
30             st.pop();
31             printf("%c",c);
32         }
33         printf("\n");
34     }
35     return 0;
36 }
dfs压栈的思维

 

通过先根和中根建树然后根据后根去遍历输出 (重写)

技术分享
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 char PD[30],ID[30];
 8 
 9 struct Node
10 {
11     Node *left;
12     Node *right;
13     char c;
14     Node(char c)
15     {
16         this->c=c;
17         this->left=left;
18         this->right=right;
19     }
20 };
21 
22 Node *root;
23 
24 Node *Create_Tree(char *pre,char *min,int len)
25 {
26     if(!len)
27         return NULL;
28     int p=0;
29     while(min[p]!=pre[0])
30         p++;
31     Node *root=new Node(pre[0]);
32     root->left=Create_Tree(pre+1,min,p);
33     root->right=Create_Tree(pre+p+1,min+p+1,len-p-1);
34     return root;
35 }
36 
37 void print_Post(Node *r)
38 {
39     if(r!=NULL)
40     {
41         print_Post(r->left);
42         print_Post(r->right);
43         printf("%c",r->c);
44     }
45 }
46 
47 int main()
48 {
49     while(cin>>PD>>ID)
50     {
51         int L=strlen(PD);
52         root=Create_Tree(PD,ID,L);
53         print_Post(root);
54         puts("");
55     }
56     return 0;
57 }
建树输出

 

直接dfs递归求解,自己抽象出栈。

技术分享
 1 #include <iostream>
 2 #include <cstring>
 3 
 4 using namespace std;
 5 char PD[30],ID[30];
 6 
 7 void dfs(int p1,int p2,int q1,int q2,int root)
 8 {
 9     if(p1>p2)
10         return;
11     for(root=q1;ID[root]!=PD[p1];++root);
12     dfs(p1+1,root-q1+p1,q1,root-1,0);
13     dfs(root+1-q1+p1,p2,root+1,q2,0);
14     cout<<ID[root];
15 }
16 
17 int main()
18 {
19     while(cin>>PD>>ID)
20     {
21         int L=strlen(PD)-1;
22         dfs(0,L,0,L,0);
23         cout<<endl;
24     }
25     return 0;
26 }
dfs重写

 

tree

标签:

原文地址:http://www.cnblogs.com/do-it-best/p/5395980.html

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