题目描述
给定一个 𝑛n 个结点的二叉树,每个结点的序号为 [1,𝑛][1,n] 中的互不相同的正整数,根结点的序号为 11。
想象自己站在二叉树的正右侧,按照从顶部到底部的顺序,求从右侧能看到的结点。
【输入格式】
第 11 行,一个正整数 𝑛n。
接下来 𝑛n 行,第 𝑖(1≤𝑖≤𝑛)i(1≤i≤n) 行两个整数 𝑙𝑖,𝑟𝑖li,ri,表示第 𝑖i 个结点的左儿子和右儿子的编号。
若某个结点在树中没有左儿子,则对应的左儿子编号为 00,右儿子缺失时同理。
【输出格式】
输出若干行,其中第 𝑖i 行输出一个整数,表示在二叉树深度为 𝑖i 的那一层中,最右侧可见的结点。
【输入输出样例#1】
输入#1
复制
7 2 3 4 0 6 7 5 0 0 0 0 0 0 0
输出#1
复制
1 3 7 5
【说明提示】
【样例解释】
二叉树如下图,其中在右边,从上到下只能看到1,3,7,5。
【数据范围】
1≤𝑛≤1041≤n≤104
思路:按顺序记录每个深度的所有节点,取最右边一个
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
vector<vector<int> > note_n(N);//标记深度为i的层节点顺序
struct tree//构造树各项数值结构体
{
int lch,rch;//左、右孩子,空时为0
int father;//父节点
int deep;//深度
}note[N];
void deep(int u)
{
if(note[u].lch)//如果u的左字节点存在将他的深度赋值为u的深度加1,note_n[note[note[u].lch].deep]也加入左子节点的号码,右节点同理
note[note[u].lch].deep=note[u].deep+1,note_n[note[note[u].lch].deep].push_back(note[u].lch),deep(note[u].lch);
if(note[u].rch)
note[note[u].rch].deep=note[u].deep+1,note_n[note[note[u].rch].deep].push_back(note[u].rch),deep(note[u].rch);
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
note_n[i].push_back(0);
for(int i=1;i<=n;i++)
note[i].father=-1;//标记父节点如果没有是-1
for(int i=1;i<=n;i++)
cin>>note[i].lch>>note[i].rch,note[note[i].lch].father=note[note[i].rch].father=i;//标记两个子节点的父亲节点是i
int root=1;//标记根节点
note[root].deep=1;
note_n[1].push_back(root);
deep(root);//列举每一项的深度
int maxdeep=0;
for(int i=1;i<=n;i++)//求树的深度
maxdeep=max(maxdeep,note[i].deep);
for(int i=1;i<=maxdeep;i++)//输出标记数组的最后一项(即最右边的节点)
cout<<note_n[i].back()<<endl;
return 0;
}