树形dp
判断一棵树是不是搜索二叉树
#include <iostream> using namespace std; const int INF=0x3f3f; typedef struct Tree { Tree(); int value; Tree *left,*right; }Tree; Tree::Tree() { value=0; left=nullptr; right=nullptr; } void create(Tree **root) { int n; cin>>n; if(n==0) *root=nullptr; else { *root=new Tree(); (*root)->left=nullptr; (*root)->right=nullptr; (*root)->value=n; create(&((*root)->left)); create(&((*root)->right)); } } typedef struct ReturnType { int size; Tree *head; int Max; int Min; ReturnType() { size=0; Max=0; Min=0; } ReturnType(int s,Tree *h,int Max,int Min) { this->size=s; this->head=h; this->Max=Max; this->Min=Min; } }ReturnType; ReturnType is_search_tree(Tree *head) { if(head==nullptr) { return ReturnType(0,nullptr,-INF,INF); } Tree *left=head->left; ReturnType LeftTreeInfo=is_search_tree(left);//1 Tree *right=head->right; ReturnType RightTreeInfo=is_search_tree(right);//2 int IncludeItSelf=0;//3 if(LeftTreeInfo.head==left&&RightTreeInfo.head==right&&head->value>LeftTreeInfo.Max&&head->value<RightTreeInfo.Min) IncludeItSelf=RightTreeInfo.size+1+LeftTreeInfo.size; int MaxSize=max(max(LeftTreeInfo.size,RightTreeInfo.size),IncludeItSelf); Tree *MaxHead=LeftTreeInfo.size>RightTreeInfo.size?LeftTreeInfo.head:RightTreeInfo.head; if(MaxSize==IncludeItSelf) MaxHead=head; return ReturnType(MaxSize,MaxHead,max(max(LeftTreeInfo.Max,RightTreeInfo.Max),head->value), min(min(RightTreeInfo.Min,LeftTreeInfo.Min),head->value)); } int main() { Tree *root=nullptr; create(&root); ReturnType returnType=is_search_tree(root); cout<<returnType.head->value<<endl; return 0; }
判断一棵树是不是平衡二叉树
//判断一棵树是否为平衡二叉树 #include <iostream> using namespace std; typedef struct Tree { int value; Tree *left,*right; Tree() { value=0; left=nullptr; right=nullptr; } }Tree; void create(Tree * &root) { int n; cin>>n; if(n==0) root=nullptr; else { root=new Tree(); root->left=nullptr; root->right=nullptr; root->value=n; create(root->left); create(root->right); } } typedef struct ReturnType { bool is_balance; int h; ReturnType(bool b,int h) { this->is_balance=b; this->h=h; } }ReturnType; ReturnType process(const Tree *head) { if(head==nullptr) return ReturnType(true,0); ReturnType leftInfo=process(head->left); if(leftInfo.is_balance==false) return ReturnType(false,-1); ReturnType rightInfo=process(head->right); if(rightInfo.is_balance==false) return ReturnType(false,-1); if(abs(rightInfo.h-leftInfo.h)>1) return ReturnType(false,-1); return ReturnType(true,max(leftInfo.h,rightInfo.h)+1); } bool is_balance(const Tree *head) { return process(head).is_balance; } int main() { Tree *root=nullptr; create(root); cout<<is_balance(root)<<endl; return 0; }
求数中任意两个结点的最远距离
//二叉树中任意两点最远的距离:求每一个节点为头的整棵树的最大距离 #include <iostream> using namespace std; typedef struct Tree { int value; Tree *left,*right; Tree() { value=0; left=nullptr; right=nullptr; } }Tree; void create(Tree **root) { int n; cin>>n; if(n==0) *root=nullptr; else { *root=new Tree(); (*root)->left=nullptr; (*root)->left=nullptr; (*root)->value=n; create(&((*root)->left)); create(&((*root)->right)); } } typedef struct ReturnType { int size; int h; ReturnType() { size=0; h=0; } ReturnType(int size,int h) { this->size=size; this->h=h; } }ReturnType; //答案肯定只来自左树,答案可能只来自右子树,答案可能来自于左右子树配合 ReturnType max_distance(Tree *head) { if(head==nullptr) { return ReturnType(0,0); } Tree *left=head->left;//1.最大距离只来自于左树 ReturnType leftInfo=max_distance(left); int p1=leftInfo.size; Tree *right=head->right;//2.最大距离只来自于右子树 ReturnType rightInfo=max_distance(right); int p2=rightInfo.size; int includeItSelf=leftInfo.h+rightInfo.h+1;//3.最大距离来自于左右子树加自己 return ReturnType(max(max(p1,p2),includeItSelf),max(leftInfo.h,rightInfo.h)+1);//最远是根据高度来判断的 } int main() { Tree *root; create(&root); cout<<max_distance(root).size<<endl; return 0; }
一个公司的上下节关系是一棵多叉树,这个公司要举办晚会,你作为组织者已经摸清了大家的心理:一个员工的直 接上级如果到场,这个员工肯定不会来。每个员工都有一个活跃度的值,决定谁来你会给这个员工发邀请函,怎么 让舞会的气氛最活跃?返回最大的活跃值。
思路:
对于这颗多叉树中的每个节点均有两种情况:
- 该节点来参加晚会,那么它的直接下级节点均不来参加。此时该节点的最大活跃值就是所有直接节点不来的活跃值相加。
- 该节点不来参加晚会,那么它的直接下级节点可以来也可以不来。此时该节点的最大活跃值是其直接节点中来或不来的活跃值中的较大值相加。
#include <iostream> #include <list> using namespace std; typedef struct Node { int activity; list<Node> list; }Node; typedef struct ReturnType { int to_act; int not_to_act; ReturnType() { to_act=0; not_to_act=0; } ReturnType(int a,int n_a) { this->to_act=a; this->not_to_act=n_a; } }ReturnType; ReturnType process(const Node &head) { int to_act=head.activity; int not_to_act=0; for(auto it=head.list.cbegin();it!=head.list.cend();++it) { Node next=(*it); ReturnType returnType=process(next); to_act+=returnType.not_to_act; not_to_act+=max(returnType.to_act,returnType.not_to_act); } return ReturnType(to_act,not_to_act); } int max_act(const Node &head) { ReturnType data=process(head); return max(data.to_act,data.not_to_act); } int main() { return 0; }