完全二叉樹應用 尋找共同父節點(vector)

2021-09-27 03:05:18 字數 845 閱讀 6587

【題意】每個師傅可以有多個徒弟,由師傅和徒弟構建一棵完全二叉樹如下:

給出多個玩家,輸出他們的最近公共師傅。比如給出d j 最近公共師傅就是b

【輸入】輸入n為結點個數,接下來以層次遍歷的方式給出n個結點。然後是x,表示x個玩家,接下來輸入x個玩家

【輸出】這x個玩家的最近公共師傅

【思路】從0開始給這個完全二叉樹標號,則標號為j的結點,其師傅有(j-1)/2,迴圈,一直到結點0。 記錄n個子節點的所有師傅(vector)father。然後遍歷x個玩家的每個師傅,給每個師傅計數為 father_count,倒敘輸出第乙個father_count為x的師傅。就是他們的最近公共師傅

#include#include#include#include#include#include#includeusing namespace std;

#define maxi 0x3f3f3f3f

std::vecto***ther[100000];//100000 nodes

int father_count[100000];

std::vectornodes_name;

int main(int argc, char const *ar**)

int j;

for(int i=1;i=0);

} int x;

cin>>x;

int t=x;

while(x--) }

return 0;

}

尋找二叉樹兩個結點的最低共同父節點

題目 二叉樹的結點的定義如下 struct treenode 輸入二叉樹中的兩個結點,輸出這兩個結點在數中最低的共同父結點。尋找二叉樹兩個結點的最低共同父節點 treenode findfirstcommonparentnode treenode proot,treenode pnodeone,tr...

二叉樹兩個結點的最低共同父節點

後序遍歷就行。尋找二叉樹兩個結點的最低共同父節點 treenode findfirstcommonparentnode treenode proot,treenode pnodeone,treenode pnodetwo if proot pnodeone proot pnodetwo treeno...

二叉樹兩結點的最低共同父結點

題目 求二叉樹兩節點的最低共同父節點 求node節點是否在head樹中 bool findnode tree head,tree node tree findlastfather tree head,tree node1,tree node2 if leftnode1 leftnode2 bool ...