#include<bits/stdc++.h>
using namespace std;
const int maxn=1000009;
vector<int> V[maxn];
int fa[maxn],ans[maxn],g[maxn];
vector<int> s[maxn];
int finf(int x){
	if(fa[x]==x)return x;
	return fa[x]=finf(fa[x]);
}
void merge(int x,int y){
	int l,r;
	if(s[x].size()<s[y].size())
		l=x,r=y;
	else
		l=y,r=x;
	int p=s[r].size()-s[l].size();
	for(int i=0;i<s[l].size();i++){
		s[r][i+p]+=s[l][i];
		if(s[r][i+p]>=s[r][ans[r]])
			ans[r]=i+p;
	}
	fa[l]=r;
}
void dfs(int x,int pre){
	fa[x]=x;
	s[x].push_back(1);
	ans[x]=0;
	for(int i:V[x]){
		if(i==pre)continue;
		dfs(i,x);
		s[finf(i)].push_back(0);
		merge(finf(x),finf(i));
	}
	int fx=finf(x);
	g[x]=s[fx].size()-1-ans[fx];
}
int main(){
	int n;
	cin>>n;
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		V[x].push_back(y);
		V[y].push_back(x);
	}

	dfs(1,0);
	for(int i=1;i<=n;i++){
		printf("%d\n",g[i]);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000009;
vector<int> V[maxn];
int n;
int ans[maxn],dep[maxn];
map<int,int> f[maxn];
void dfs(int x,int pre){
	dep[x]=dep[pre]+1;
	ans[x]=dep[x];
	f[x][dep[x]]++;
	for(int i:V[x]){
		if(i==pre)continue;
		dfs(i,x);
		for(auto &j:f[i]){
			int deep=j.first;
			f[x][deep]+=j.second;
			if(f[x][deep]>f[x][ans[x]])
				ans[x]=deep;
			else if(f[x][deep]==f[x][ans[x]])
				ans[x]=min(ans[x],deep);
		}
	}
}
int main(){
	cin>>n;

	//22
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		V[x].push_back(y);
		V[y].push_back(x);
	}
	dep[0]=-1;
	dfs(1,0);
	for(int i=1;i<=n;i++){
		printf("%d\n",ans[i]-dep[i]);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000009;
vector<int> V[maxn];
int n;
int ans[maxn],dep[maxn];
map<int,int> f[maxn];
void dfs(int x,int pre){
	dep[x]=dep[pre]+1;
	ans[x]=dep[x];
	f[x][dep[x]]++;
	for(int i:V[x]){
		if(i==pre)continue;
		dfs(i,x);
		for(auto &j:f[i]){
			int deep=j.first;
			f[x][deep]+=j.second;
			if(f[x][deep]>f[x][ans[x]])
				ans[x]=deep;
			else if(f[x][deep]==f[x][ans[x]])
				ans[x]=min(ans[x],deep);
		}
	}
	f[x].clear();
}
int main(){
	cin>>n;

	//22
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		V[x].push_back(y);
		V[y].push_back(x);
	}
	dep[0]=-1;
	dfs(1,0);
	for(int i=1;i<=n;i++){
		printf("%d\n",ans[i]-dep[i]);
	}
	return 0;
}


 //Dragon_warrior7(IITK)
#pragma GCC optimize("O0")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("-ffloat-store")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define ll long long
#define pb push_back
#define nl cout << '\n'
#define sor(x) sort(x.begin(), x.end())
#define rev(v) reverse(v.begin(), v.end())
#define lb(v, temp) lower_bound(v.begin(), v.end(), temp)
#define ub(v, temp) upper_bound(v.begin(), v.end(), temp)
#define fi first
#define se second
#define llmax 1000000000000000000
#define pll pair<ll, ll>
#define vll vector<ll>
#define rub cerr << "\n------------------------------------\n"
#define deb(x) cerr<<#x<<' '<<'='<<' '<<x<<'\n'
#define Lets_be_precise cout<<fixed<<setprecision(10)
//typedef  tree < int ,  null_type ,  greater<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > oset;
// const int mod=1e9+7;
// ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// ll modInverse(ll a){return power(a,mod-2);}
ll n,m,a,b,c,k,temp,x,y;
const int MAXN=1000000+11;
inline void read(vector<ll> &v);
inline ll max(ll a,ll b);
inline ll min(ll a,ll b);
inline ll gcd(ll a ,ll b);
void storepowers(int n,vector<int> &a);
#define debug 0
int depth[1000000+1];
int sz[1000000+1];
vector<int> v[1000000+1];
int bigc[1000000+1];
int mx;
void precalc(int u,int p=-1)
{
   sz[u]=1;
   if(p!=-1)
   depth[u]=depth[p]+1;
   mx=-1;
   bigc[u]=-1;
   for(auto x:v[u])
   {
       if(x==p)
       continue;
       precalc(x,u);
       sz[u]+=sz[x];
       if(mx<sz[x])
       {
           mx=sz[x];
           bigc[u]=x;
       }
   }
}
int ans[1000000+1];
vector<int> vec[MAXN];
int t[4*MAXN];
int get_max(int v, int tl, int tr, int l, int r)
{
    if (l > r)
        return 0;
    if (l == tl && r == tr)
    {
        return t[v];
    }
    int tm = (tl + tr) / 2;
    return max(get_max(v*2, tl, tm, l, min(r, tm))
           ,get_max(v*2+1, tm+1, tr, max(l, tm+1), r));
}
void update(int v, int tl, int tr, int pos, int new_val) {
    if (tl == tr) {
        t[v] = t[v]+new_val;
    } else
    {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            update(v*2, tl, tm, pos, new_val);
        else
            update(v*2+1, tm+1, tr, pos, new_val);
        t[v] = max(t[v*2],t[v*2+1]);
    }
}
int get_first(int v, int lv, int rv, int l, int r, int x) {
    if(lv > r || rv < l) return -1;
    if(l <= lv && rv <= r) {
        if(t[v] <= x) return -1;
        while(lv != rv) {
            int mid = lv + (rv-lv)/2;
            if(t[2*v] > x) {
                v = 2*v;
                rv = mid;
            }else {
                v = 2*v+1;
                lv = mid+1;
            }
        }
        return lv;
    }
    int mid = lv + (rv-lv)/2;
    int rs = get_first(2*v, lv, mid, l, r, x);
    if(rs != -1) return rs;
    return get_first(2*v+1, mid+1, rv, l ,r, x);
}
void dfs(int u,int p,bool keep)
{
  for(auto x:v[u])
  {
      if(x!=p && x!=bigc[u])
      dfs(x,u,0);
  }
  if(bigc[u]!=-1)
  {
      dfs(bigc[u],u,1);
      swap(vec[u],vec[bigc[u]]);
  }
  vec[u].pb(u);
  update(1,0,n-1,depth[u],1);
  for (auto x : v[u])
  {
		if (x != p && x != bigc[u]) {
			for (auto y : vec[x])
			{
			    update(1,0,n-1,depth[y],1);
				vec[u].push_back(y);
			}
		}
	}
	ll mx=get_max(1,0,n-1,0,n-1);
	ans[u]=get_first(1,0,n-1,depth[u],n-1,mx-1)-depth[u];
  if (keep == 0)
  {
		for (auto x : vec[u])
		{
		        update(1,0,n-1,depth[x],-1);
		}
  }
}
//#define cerr if(false)cerr//uncomment to remove cerr from program...
void solveforthiscase(const int & test)
{if(debug)cout<<"Case #"<<test<<':'<<'\n';
cin>>n;
depth[1]=0;
m=n-1;
while(m--)
{
    cin>>a>>b;
    v[a].pb(b);
    v[b].pb(a);
}
precalc(1);
dfs(1,-1,0);
for(int i=1;i<=n;i++)
cout<<ans[i]<<' ';
if(debug)cout<<'\n';
return ;
}
int main()
{
	ios_base::sync_with_stdio(false);
	Lets_be_precise;
	cin.tie(NULL);
	cout.tie(NULL);
	int test=1;
 	//cin>>test;
    for(int i=1;i<=test;i++)
    {
        solveforthiscase(i);
    }
}
void storepowers(int n,vector<int> &a)
{
    a.resize(n+1);
    a[0]=1;
    for(int i=1;i<=n;i++)
    a[i]=a[i-1]*2;
    return ;
}
inline ll max(ll a,ll b)
{return ((a>b)?a:b);}
inline ll min(ll a,ll b)
{return ((a>b)?b:a);}
inline ll gcd(ll a ,ll b)
{if(b==0) return a;a%=b;return gcd(b,a);}
inline void read(vector<ll> &v)
{for (int i = 0; i < v.size(); i++)cin>>v[i];}


 //Dragon_warrior7(IITK)
#pragma GCC optimize("O0")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("-ffloat-store")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define ll long long
#define pb push_back
#define nl cout << '\n'
#define sor(x) sort(x.begin(), x.end())
#define rev(v) reverse(v.begin(), v.end())
#define lb(v, temp) lower_bound(v.begin(), v.end(), temp)
#define ub(v, temp) upper_bound(v.begin(), v.end(), temp)
#define fi first
#define se second
#define llmax 1000000000000000000
#define pll pair<ll, ll>
#define vll vector<ll>
#define rub cerr << "\n------------------------------------\n"
#define deb(x) cerr<<#x<<' '<<'='<<' '<<x<<'\n'
#define Lets_be_precise cout<<fixed<<setprecision(10)
//typedef  tree < int ,  null_type ,  greater<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > oset;
// const int mod=1e9+7;
// ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// ll modInverse(ll a){return power(a,mod-2);}
ll n,m,a,b,c,k,temp,x,y;
const int MAXN=1000000+11;
inline void read(vector<ll> &v);
inline ll max(ll a,ll b);
inline ll min(ll a,ll b);
inline ll gcd(ll a ,ll b);
void storepowers(int n,vector<int> &a);
#define debug 0
int depth[MAXN];
int sz[MAXN];
vll v[MAXN];
void precalc(int u,int p=-1)
{
   sz[u]=1;
   if(p!=-1)
   depth[u]=depth[p]+1;
   for(auto x:v[u])
   {
       if(x==p)
       continue;
       precalc(x,u);
       sz[u]+=sz[x];
   }
}
int ans[MAXN];
vector<int> vec[MAXN];
set<pll> cnt;
int counter[MAXN];
void dfs(int u,int p,bool keep)
{
  int mx=-1;
  int bigc=-1;
  for(auto x:v[u])
  {
      if(x!=p && mx<sz[x])
      {
          mx=sz[x];
          bigc=x;
      }
  }
  for(auto x:v[u])
  {
      if(x!=p && x!=bigc)
      dfs(x,u,0);
  }
  if(bigc!=-1)
  {
      dfs(bigc,u,1);
      swap(vec[u],vec[bigc]);
  }
  vec[u].pb(u);
  counter[depth[u]]++;
  cnt.erase({counter[depth[u]]-1,depth[u]});
  cnt.insert({counter[depth[u]],depth[u]});
  for (auto x : v[u])
  {
		if (x != p && x != bigc) {
			for (auto y : vec[x])
			{
				counter[depth[x]]++;
				cnt.erase({counter[depth[x]]-1,depth[x]});
                cnt.insert({counter[depth[x]],depth[x]});
				vec[u].push_back(x);
			}
		}
	}
	ll mxcount=(*cnt.rbegin()).fi;
	pll pr={mxcount,0};
	auto it=cnt.lower_bound(pr);
	ans[u]=(*it).se-depth[u];
  if (keep == 0)
  {
		for (auto x : vec[u])
		{
		        counter[depth[x]]--;
				cnt.erase({counter[depth[x]]+1,depth[x]});
				cnt.insert({counter[depth[x]],depth[x]});
		}
  }
}
//#define cerr if(false)cerr//uncomment to remove cerr from program...
void solveforthiscase(const int & test)
{if(debug)cout<<"Case #"<<test<<':'<<'\n';
cin>>n;
depth[1]=0;
m=n-1;
while(m--)
{
    cin>>a>>b;
    v[a].pb(b);
    v[b].pb(a);
}
precalc(1);
for(int i=0;i<MAXN;i++)
cnt.insert({0,i});
dfs(1,-1,0);
for(int i=1;i<=n;i++)
cout<<ans[i]<<' ';
if(debug)cout<<'\n';
return ;
}
int main()
{
	ios_base::sync_with_stdio(false);
	Lets_be_precise;
	cin.tie(NULL);
	cout.tie(NULL);
	int test=1;
 	//cin>>test;
    for(int i=1;i<=test;i++)
    {
        solveforthiscase(i);
    }
}
void storepowers(int n,vector<int> &a)
{
    a.resize(n+1);
    a[0]=1;
    for(int i=1;i<=n;i++)
    a[i]=a[i-1]*2;
    return ;
}
inline ll max(ll a,ll b)
{return ((a>b)?a:b);}
inline ll min(ll a,ll b)
{return ((a>b)?b:a);}
inline ll gcd(ll a ,ll b)
{if(b==0) return a;a%=b;return gcd(b,a);}
inline void read(vector<ll> &v)
{for (int i = 0; i < v.size(); i++)cin>>v[i];}


 //Dragon_warrior7(IITK)
#pragma GCC optimize("O0")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("-ffloat-store")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define ll long long
#define pb push_back
#define nl cout << '\n'
#define sor(x) sort(x.begin(), x.end())
#define rev(v) reverse(v.begin(), v.end())
#define lb(v, temp) lower_bound(v.begin(), v.end(), temp)
#define ub(v, temp) upper_bound(v.begin(), v.end(), temp)
#define fi first
#define se second
#define llmax 1000000000000000000
#define pll pair<ll, ll>
#define vll vector<ll>
#define rub cerr << "\n------------------------------------\n"
#define deb(x) cerr<<#x<<' '<<'='<<' '<<x<<'\n'
#define Lets_be_precise cout<<fixed<<setprecision(10)
//typedef  tree < int ,  null_type ,  greater<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > oset;
// const int mod=1e9+7;
// ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// ll modInverse(ll a){return power(a,mod-2);}
ll n,m,a,b,c,k,temp,x,y;
const int MAXN=1000000+11;
inline void read(vector<ll> &v);
inline ll max(ll a,ll b);
inline ll min(ll a,ll b);
inline ll gcd(ll a ,ll b);
void storepowers(int n,vector<int> &a);
#define debug 0
int depth[1000000+1];
int sz[1000000+1];
vector<int> v[1000000+1];
int bigc[1000000+1];
int mx;
void precalc(int u,int p=-1)
{
   sz[u]=1;
   if(p!=-1)
   depth[u]=depth[p]+1;
   mx=-1;
   bigc[u]=-1;
   for(auto x:v[u])
   {
       if(x==p)
       continue;
       precalc(x,u);
       sz[u]+=sz[x];
       if(mx<sz[x])
       {
           mx=sz[x];
           bigc[u]=x;
       }
   }
}
int ans[1000000+1];
vector<int> vec[MAXN];
struct classcomp
{
  bool operator() (const pair<int,int> &lhs, const pair<int,int> &rhs) const
  {if(lhs.fi>rhs.fi)
      return 1;
      if(lhs.fi==rhs.fi && lhs.se<rhs.se)
      return 1;
      return 0;
  }
};
set<pair<int,int>,classcomp> cnt;
int counter[MAXN];
void dfs(int u,int p,bool keep)
{
  for(auto x:v[u])
  {
      if(x!=p && x!=bigc[u])
      dfs(x,u,0);
  }
  if(bigc[u]!=-1)
  {
      dfs(bigc[u],u,1);
      swap(vec[u],vec[bigc[u]]);
  }
  vec[u].pb(u);
  counter[depth[u]]++;
  cnt.erase({counter[depth[u]]-1,depth[u]});
  cnt.insert({counter[depth[u]],depth[u]});
  for (auto x : v[u])
  {
		if (x != p && x != bigc[u]) {
			for (auto y : vec[x])
			{
				counter[depth[y]]++;
				cnt.erase({counter[depth[y]]-1,depth[y]});
                cnt.insert({counter[depth[y]],depth[y]});
				vec[u].push_back(y);
			}
		}
	}
	ans[u]=(*cnt.begin()).se-depth[u];
  if (keep == 0)
  {
		for (auto x : vec[u])
		{
		        counter[depth[x]]--;
				cnt.erase({counter[depth[x]]+1,depth[x]});
				cnt.insert({counter[depth[x]],depth[x]});
		}
  }
}
//#define cerr if(false)cerr//uncomment to remove cerr from program...
void solveforthiscase(const int & test)
{if(debug)cout<<"Case #"<<test<<':'<<'\n';
cin>>n;
depth[1]=0;
m=n-1;
while(m--)
{
    cin>>a>>b;
    v[a].pb(b);
    v[b].pb(a);
}
precalc(1);
dfs(1,-1,0);
for(int i=1;i<=n;i++)
cout<<ans[i]<<' ';
if(debug)cout<<'\n';
return ;
}
int main()
{
	ios_base::sync_with_stdio(false);
	Lets_be_precise;
	cin.tie(NULL);
	cout.tie(NULL);
	int test=1;
 	//cin>>test;
    for(int i=1;i<=test;i++)
    {
        solveforthiscase(i);
    }
}
void storepowers(int n,vector<int> &a)
{
    a.resize(n+1);
    a[0]=1;
    for(int i=1;i<=n;i++)
    a[i]=a[i-1]*2;
    return ;
}
inline ll max(ll a,ll b)
{return ((a>b)?a:b);}
inline ll min(ll a,ll b)
{return ((a>b)?b:a);}
inline ll gcd(ll a ,ll b)
{if(b==0) return a;a%=b;return gcd(b,a);}
inline void read(vector<ll> &v)
{for (int i = 0; i < v.size(); i++)cin>>v[i];}


 //Dragon_warrior7(IITK)
#pragma GCC optimize("O0")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("-ffloat-store")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define ll long long
#define pb push_back
#define nl cout << '\n'
#define sor(x) sort(x.begin(), x.end())
#define rev(v) reverse(v.begin(), v.end())
#define lb(v, temp) lower_bound(v.begin(), v.end(), temp)
#define ub(v, temp) upper_bound(v.begin(), v.end(), temp)
#define fi first
#define se second
#define llmax 1000000000000000000
#define pll pair<ll, ll>
#define vll vector<ll>
#define rub cerr << "\n------------------------------------\n"
#define deb(x) cerr<<#x<<' '<<'='<<' '<<x<<'\n'
#define Lets_be_precise cout<<fixed<<setprecision(10)
//typedef  tree < int ,  null_type ,  greater<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > oset;
// const int mod=1e9+7;
// ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// ll modInverse(ll a){return power(a,mod-2);}
ll n,m,a,b,c,k,temp,x,y;
const int MAXN=1000000+11;
inline void read(vector<ll> &v);
inline ll max(ll a,ll b);
inline ll min(ll a,ll b);
inline ll gcd(ll a ,ll b);
void storepowers(int n,vector<int> &a);
#define debug 0
int depth[MAXN];
int sz[MAXN];
vector<int> v[MAXN];
void precalc(int u,int p=-1)
{
   sz[u]=1;
   if(p!=-1)
   depth[u]=depth[p]+1;
   for(auto x:v[u])
   {
       if(x==p)
       continue;
       precalc(x,u);
       sz[u]+=sz[x];
   }
}
int ans[MAXN];
vector<int> vec[MAXN];
set<pll> cnt;
int counter[MAXN];
void dfs(int u,int p,bool keep)
{
  int mx=-1;
  int bigc=-1;
  for(auto x:v[u])
  {
      if(x!=p && mx<sz[x])
      {
          mx=sz[x];
          bigc=x;
      }
  }
  for(auto x:v[u])
  {
      if(x!=p && x!=bigc)
      dfs(x,u,0);
  }
  if(bigc!=-1)
  {
      dfs(bigc,u,1);
      swap(vec[u],vec[bigc]);
  }
  vec[u].pb(u);
  counter[depth[u]]++;
  cnt.erase({counter[depth[u]]-1,depth[u]});
  cnt.insert({counter[depth[u]],depth[u]});
  for (auto x : v[u])
  {
		if (x != p && x != bigc) {
			for (auto y : vec[x])
			{
				counter[depth[y]]++;
				cnt.erase({counter[depth[y]]-1,depth[y]});
                cnt.insert({counter[depth[y]],depth[y]});
				vec[u].push_back(y);
			}
		}
	}
	ll mxcount=(*cnt.rbegin()).fi;
	pll pr={mxcount,0};
	auto it=cnt.lower_bound(pr);
	ans[u]=(*it).se-depth[u];
  if (keep == 0)
  {
		for (auto x : vec[u])
		{
		        counter[depth[x]]--;
				cnt.erase({counter[depth[x]]+1,depth[x]});
				cnt.insert({counter[depth[x]],depth[x]});
		}
  }
}
//#define cerr if(false)cerr//uncomment to remove cerr from program...
void solveforthiscase(const int & test)
{if(debug)cout<<"Case #"<<test<<':'<<'\n';
cin>>n;
depth[1]=0;
m=n-1;
while(m--)
{
    cin>>a>>b;
    v[a].pb(b);
    v[b].pb(a);
}
precalc(1);
for(int i=0;i<n;i++)
cnt.insert({0,i});
dfs(1,-1,0);
for(int i=1;i<=n;i++)
cout<<ans[i]<<' ';
if(debug)cout<<'\n';
return ;
}
int main()
{
	ios_base::sync_with_stdio(false);
	Lets_be_precise;
	cin.tie(NULL);
	cout.tie(NULL);
	int test=1;
 	//cin>>test;
    for(int i=1;i<=test;i++)
    {
        solveforthiscase(i);
    }
}
void storepowers(int n,vector<int> &a)
{
    a.resize(n+1);
    a[0]=1;
    for(int i=1;i<=n;i++)
    a[i]=a[i-1]*2;
    return ;
}
inline ll max(ll a,ll b)
{return ((a>b)?a:b);}
inline ll min(ll a,ll b)
{return ((a>b)?b:a);}
inline ll gcd(ll a ,ll b)
{if(b==0) return a;a%=b;return gcd(b,a);}
inline void read(vector<ll> &v)
{for (int i = 0; i < v.size(); i++)cin>>v[i];}


 //Dragon_warrior7(IITK)
#pragma GCC optimize("O0")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("-ffloat-store")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define ll long long
#define pb push_back
#define nl cout << '\n'
#define sor(x) sort(x.begin(), x.end())
#define rev(v) reverse(v.begin(), v.end())
#define lb(v, temp) lower_bound(v.begin(), v.end(), temp)
#define ub(v, temp) upper_bound(v.begin(), v.end(), temp)
#define fi first
#define se second
#define llmax 1000000000000000000
#define pll pair<ll, ll>
#define vll vector<ll>
#define rub cerr << "\n------------------------------------\n"
#define deb(x) cerr<<#x<<' '<<'='<<' '<<x<<'\n'
#define Lets_be_precise cout<<fixed<<setprecision(10)
//typedef  tree < int ,  null_type ,  greater<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > oset;
// const int mod=1e9+7;
// ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// ll modInverse(ll a){return power(a,mod-2);}
ll n,m,a,b,c,k,temp,x,y;
const int MAXN=1000000+11;
inline void read(vector<ll> &v);
inline ll max(ll a,ll b);
inline ll min(ll a,ll b);
inline ll gcd(ll a ,ll b);
void storepowers(int n,vector<int> &a);
#define debug 0
int depth[1000000+1];
int sz[1000000+1];
vector<int> v[1000000+1];
int bigc[1000000+1];
int mx;
void precalc(int u,int p=-1)
{
   sz[u]=1;
   if(p!=-1)
   depth[u]=depth[p]+1;
   mx=-1;
   bigc[u]=-1;
   for(auto x:v[u])
   {
       if(x==p)
       continue;
       precalc(x,u);
       sz[u]+=sz[x];
       if(mx<sz[x])
       {
           mx=sz[x];
           bigc[u]=x;
       }
   }
}
int ans[1000000+1];
vector<int> vec[MAXN];
int cnt[MAXN];
void dfs(int u,int p,bool keep)
{
  for(auto x:v[u])
  {
      if(x!=p && x!=bigc[u])
      dfs(x,u,0);
  }
  if(bigc[u]!=-1)
  {
      dfs(bigc[u],u,1);
      swap(vec[u],vec[bigc[u]]);
  }
  vec[u].pb(u);
  cnt[depth[u]]++;
  pll pr={cnt[depth[u]],depth[u]};
  for (auto x : v[u])
  {
		if (x != p && x != bigc[u]) {
			for (auto y : vec[x])
			{
			    cnt[depth[y]]++;
			    if(cnt[depth[y]]>pr.fi)
			    {
			        pr.fi=cnt[depth[y]];
			        pr.se=depth[y];
			    }
			    else if(cnt[depth[y]]==pr.fi && pr.se>depth[y])
			    {
			        pr.se=depth[y];
			    }
				vec[u].push_back(y);
			}
		}
	}
	{
	int bigans;
	if(bigc[u]!=-1)
	{
	bigans=ans[bigc[u]]+depth[bigc[u]];
	if(cnt[bigans]>pr.fi)
	{pr.fi=cnt[bigans];
	pr.se=bigans;}
	else if(cnt[bigans]==pr.fi && pr.se>bigans)
	pr.se=bigans;
	}
	}
	ans[u]=pr.se-depth[u];
  if (keep == 0)
  {
		for (auto x : vec[u])
		{
		        cnt[depth[x]]--;
		}
  }
}
//#define cerr if(false)cerr//uncomment to remove cerr from program...
void solveforthiscase(const int & test)
{if(debug)cout<<"Case #"<<test<<':'<<'\n';
cin>>n;
depth[1]=0;
m=n-1;
while(m--)
{
    cin>>a>>b;
    v[a].pb(b);
    v[b].pb(a);
}
precalc(1);
dfs(1,-1,0);
for(int i=1;i<=n;i++)
cout<<ans[i]<<' ';
if(debug)cout<<'\n';
return ;
}
int main()
{
	ios_base::sync_with_stdio(false);
	Lets_be_precise;
	cin.tie(NULL);
	cout.tie(NULL);
	int test=1;
 	//cin>>test;
    for(int i=1;i<=test;i++)
    {
        solveforthiscase(i);
    }
}
void storepowers(int n,vector<int> &a)
{
    a.resize(n+1);
    a[0]=1;
    for(int i=1;i<=n;i++)
    a[i]=a[i-1]*2;
    return ;
}
inline ll max(ll a,ll b)
{return ((a>b)?a:b);}
inline ll min(ll a,ll b)
{return ((a>b)?b:a);}
inline ll gcd(ll a ,ll b)
{if(b==0) return a;a%=b;return gcd(b,a);}
inline void read(vector<ll> &v)
{for (int i = 0; i < v.size(); i++)cin>>v[i];}


 //Dragon_warrior7(IITK)
#pragma GCC optimize("O0")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("-ffloat-store")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define ll long long
#define pb push_back
#define nl cout << '\n'
#define sor(x) sort(x.begin(), x.end())
#define rev(v) reverse(v.begin(), v.end())
#define lb(v, temp) lower_bound(v.begin(), v.end(), temp)
#define ub(v, temp) upper_bound(v.begin(), v.end(), temp)
#define fi first
#define se second
#define llmax 1000000000000000000
#define pll pair<ll, ll>
#define vll vector<ll>
#define rub cerr << "\n------------------------------------\n"
#define deb(x) cerr<<#x<<' '<<'='<<' '<<x<<'\n'
#define Lets_be_precise cout<<fixed<<setprecision(10)
//typedef  tree < int ,  null_type ,  greater<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > oset;
// const int mod=1e9+7;
// ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// ll modInverse(ll a){return power(a,mod-2);}
ll n,m,a,b,c,k,temp,x,y;
const int MAXN=1000000+11;
inline void read(vector<ll> &v);
inline ll max(ll a,ll b);
inline ll min(ll a,ll b);
inline ll gcd(ll a ,ll b);
void storepowers(int n,vector<int> &a);
#define debug 0
int depth[1000000];
int sz[1000000+1];
vector<int> v[1000000+1];
void precalc(int u,int p=-1)
{
   sz[u]=1;
   if(p!=-1)
   depth[u]=depth[p]+1;
   for(auto x:v[u])
   {
       if(x==p)
       continue;
       precalc(x,u);
       sz[u]+=sz[x];
   }
}
int ans[1000000+1];
vector<int> vec[1000000+1];
int cnt[1000000+1];
pair<int,int> pr;
void dfs(int u,int p,bool keep)
{
    int mx=-1;
    int bigc=-1;
    for(auto x:v[u])
    {
       if(x!=p && sz[x]>mx)
       {
           mx=sz[x];
           bigc=x;
       }
    }
  for(auto x:v[u])
  {
      if(x!=p && x!=bigc)
      dfs(x,u,0);
  }
  if(bigc!=-1)
  {
      dfs(bigc,u,1);
      swap(vec[u],vec[bigc]);
  }
  vec[u].pb(u);
  cnt[depth[u]]++;
  pr={cnt[depth[u]],depth[u]};
  for (auto x : v[u])
  {
		if (x != p && x != bigc) {
			for (auto y : vec[x])
			{
			    cnt[depth[y]]++;
			    if(cnt[depth[y]]>pr.fi)
			    {
			        pr.fi=cnt[depth[y]];
			        pr.se=depth[y];
			    }
			    else if(cnt[depth[y]]==pr.fi && pr.se>depth[y])
			    {
			        pr.se=depth[y];
			    }
				vec[u].push_back(y);
			}
		}
	}
	int bigans=0;
	if(bigc!=-1)
	{
	bigans=ans[bigc]+depth[bigc];
	if(cnt[bigans]>pr.fi)
	{pr.fi=cnt[bigans];
	pr.se=bigans;}
	else if(cnt[bigans]==pr.fi && pr.se>bigans)
	pr.se=bigans;
	}
	ans[u]=pr.se-depth[u];
  if (keep == 0)
  {
		for (auto x : vec[u])
		{
		        cnt[depth[x]]--;
		}
  }
}
//#define cerr if(false)cerr//uncomment to remove cerr from program...
void solveforthiscase(const int & test)
{if(debug)cout<<"Case #"<<test<<':'<<'\n';
cin>>n;
depth[1]=0;
m=n-1;
while(m--)
{
    cin>>a>>b;
    v[a].pb(b);
    v[b].pb(a);
}
precalc(1);
dfs(1,-1,0);
for(int i=1;i<=n;i++)
cout<<ans[i]<<' ';
if(debug)cout<<'\n';
return ;
}
int main()
{
	ios_base::sync_with_stdio(false);
	Lets_be_precise;
	cin.tie(NULL);
	cout.tie(NULL);
	int test=1;
 	//cin>>test;
    for(int i=1;i<=test;i++)
    {
        solveforthiscase(i);
    }
}
void storepowers(int n,vector<int> &a)
{
    a.resize(n+1);
    a[0]=1;
    for(int i=1;i<=n;i++)
    a[i]=a[i-1]*2;
    return ;
}
inline ll max(ll a,ll b)
{return ((a>b)?a:b);}
inline ll min(ll a,ll b)
{return ((a>b)?b:a);}
inline ll gcd(ll a ,ll b)
{if(b==0) return a;a%=b;return gcd(b,a);}
inline void read(vector<ll> &v)
{for (int i = 0; i < v.size(); i++)cin>>v[i];}


#include <bits/stdc++.h>

template <typename T> inline void rd(T& x) {
	int si = 1; char c = getchar(); x = 0;
	while(!isdigit(c)) si = c == '-' ? -1 : si, c = getchar();
	while(isdigit(c)) x = x * 10 + c - 48, c = getchar();
	x *= si;
}
template <typename T, typename... Args>
inline void rd(T& x, Args&... args) { rd(x); rd(args...); }
#define fi first
#define se second
#define mkp std::make_pair
#define V std::vector
#define H std::priority_queue
typedef long long ll;
typedef double ff;
typedef std::pair <int, int> pii;
const int kN = 1e6 + 5, kInf = 0x3f3f3f3f;
const ll kMod = 998244353, kInfLL = 0x3f3f3f3f3f3f3f3fLL;

int n, dep[kN], hvy[kN], fa[kN], ans[kN];
std::vector <int> E[kN], f[kN];
void Add(int u, int v) { E[u].push_back(v); }

void Dfs(int cur) {
	dep[cur] = 1;
	for(auto i : E[cur])
		if(i != fa[cur]) {
			fa[i] = cur; Dfs(i);
			dep[cur] = std::max(dep[i] + 1, dep[cur]);
			if(dep[i] > dep[hvy[cur]])
				hvy[cur] = i;
		}
}
int Solve(int cur, int tar) {
	//printf("%d %d %d\n", cur, tar, hvy[cur]);
	int cur_ans;
	if(hvy[cur]) cur_ans = Solve(hvy[cur], cur);
	else cur_ans = 0;
	for(auto i : E[cur]) {
		if(i != fa[cur] && i != hvy[cur]) {
			Solve(i, i);
		//	if(f[cur].size() < f[i].size()) exit(0);
		//	std::cout << cur << " " << i << " " << f[cur].size() << " " << f[i].size() << std::endl;
			for(int j = 0; j < f[i].size(); ++j) {
				int cur_j = j + f[cur].size() - f[i].size();
				f[cur][cur_j] += f[i][j];
				if(f[cur][cur_j] > f[cur][cur_ans] ||
					(f[cur][cur_j] == f[cur][cur_ans] && cur_j > cur_ans))
						cur_ans = cur_j;
			}
			f[i].resize(0);
		}
	}
	f[cur].push_back(1);
	if(f[cur][cur_ans] == 1) cur_ans = f[cur].size() - 1;
	ans[cur] = f[cur].size() - cur_ans - 1;
	//printf("%d : ", cur);
	//for(auto i : f[cur]) printf("%d ", i);
	//printf("\n");
	std::swap(f[cur], f[tar]);
	return cur_ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
#endif
	int T = 1; //rd(T);
	while(T--) {
		rd(n);
		for(int i = 1; i < n; ++i) {
			int u, v; rd(u, v);
			Add(u, v); Add(v, u);
		}
		Dfs(1);
		//for(int i = 1; i <= n; ++i) printf("%d ", hvy[i]);
		//printf("\n");
		Solve(1, 1);
		for(int i = 1; i <= n; ++i)
			printf("%d\n", ans[i]);
	} return 0;
}


#include <bits/stdc++.h>

template <typename T> inline void rd(T& x) {
	int si = 1; char c = getchar(); x = 0;
	while(!isdigit(c)) si = c == '-' ? -1 : si, c = getchar();
	while(isdigit(c)) x = x * 10 + c - 48, c = getchar();
	x *= si;
}
template <typename T, typename... Args>
inline void rd(T& x, Args&... args) { rd(x); rd(args...); }
#define fi first
#define se second
#define mkp std::make_pair
#define V std::vector
#define H std::priority_queue
typedef long long ll;
typedef double ff;
typedef std::pair <int, int> pii;
const int kN = 1e6 + 5, kInf = 0x3f3f3f3f;
const ll kMod = 998244353, kInfLL = 0x3f3f3f3f3f3f3f3fLL;

int n, dep[kN], hvy[kN], fa[kN], ans[kN];
std::vector <int> E[kN], f[kN];
void Add(int u, int v) { E[u].push_back(v); }

void Dfs(int cur) {
	dep[cur] = 1;
	for(auto i : E[cur])
		if(i != fa[cur]) {
			fa[i] = cur; Dfs(i);
			dep[cur] = std::max(dep[i] + 1, dep[cur]);
			if(dep[i] > dep[hvy[cur]])
				hvy[cur] = i;
		}
}
int Solve(int cur, int tar) {
	int cur_ans;
	if(hvy[cur]) cur_ans = Solve(hvy[cur], cur);
	else cur_ans = 0;
	for(auto i : E[cur]) {
		if(i != fa[cur] && i != hvy[cur]) {
			Solve(i, i);
			int dlt = f[cur].size() - f[i].size();
			for(int j = 0; j < f[i].size(); ++j) {
				int cur_j = j + dlt;
				f[cur][cur_j] += f[i][j];
				if(f[cur][cur_j] > f[cur][cur_ans] ||
					(f[cur][cur_j] == f[cur][cur_ans] && cur_j > cur_ans))
						cur_ans = cur_j;
			}
			f[i].resize(0);
		}
	}
	f[cur].push_back(1);
	if(f[cur][cur_ans] == 1) cur_ans = f[cur].size() - 1;
	ans[cur] = f[cur].size() - cur_ans - 1;
	std::swap(f[cur], f[tar]);
	return cur_ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
#endif
	int T = 1; //rd(T);
	while(T--) {
		rd(n);
		for(int i = 1; i < n; ++i) {
			int u, v; rd(u, v);
			Add(u, v); Add(v, u);
		}
		Dfs(1); Solve(1, 1);
		for(int i = 1; i <= n; ++i)
			printf("%d\n", ans[i]);
	} return 0;
}


#include <bits/stdc++.h>

template <typename T> inline void rd(T& x) {
	int si = 1; char c = getchar(); x = 0;
	while(!isdigit(c)) si = c == '-' ? -1 : si, c = getchar();
	while(isdigit(c)) x = x * 10 + c - 48, c = getchar();
	x *= si;
}
template <typename T, typename... Args>
inline void rd(T& x, Args&... args) { rd(x); rd(args...); }
#define fi first
#define se second
#define mkp std::make_pair
#define V std::vector
#define H std::priority_queue
typedef long long ll;
typedef double ff;
typedef std::pair <int, int> pii;
const int kN = 1e6 + 5, kInf = 0x3f3f3f3f;
const ll kMod = 998244353, kInfLL = 0x3f3f3f3f3f3f3f3fLL;

int n, dep[kN], hvy[kN], fa[kN], ans[kN];
std::vector <int> E[kN], f[kN];
void Add(int u, int v) { E[u].push_back(v); }

void Dfs(int cur) {
	for(auto i : E[cur])
		if(i != fa[cur]) {
			dep[i] = dep[cur] + 1;
			fa[i] = cur; Dfs(i);
			if(dep[i] > dep[hvy[cur]])
				hvy[cur] = i;
		}
}
int Solve(int cur, int tar) {
	//printf("%d %d %d\n", cur, tar, hvy[cur]);
	int cur_ans;
	if(hvy[cur]) cur_ans = Solve(hvy[cur], cur);
	else cur_ans = 0;
	for(auto i : E[cur]) {
		if(i != fa[cur] && i != hvy[cur]) {
			Solve(i, i);
			if(f[cur].size() < f[i].size()) exit(0);
		//	std::cout << cur << " " << i << " " << f[cur].size() << " " << f[i].size() << std::endl;
			for(int j = 0; j < f[i].size(); ++j) {
				f[cur][j] += f[i][j];
				if(f[cur][j] > f[cur][cur_ans] ||
					(f[cur][j] == f[cur][cur_ans] && j > cur_ans)) cur_ans = j;
			}
			f[i].resize(0);
		}
	}
	f[cur].push_back(1);
	if(f[cur][cur_ans] == 1) cur_ans = f[cur].size() - 1;
	ans[cur] = f[cur].size() - cur_ans - 1;
	//printf("%d : ", cur);
	//for(auto i : f[cur]) printf("%d ", i);
	//printf("\n");
	std::swap(f[cur], f[tar]);
	return cur_ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
#endif
	int T = 1; //rd(T);
	while(T--) {
		rd(n);
		for(int i = 1; i < n; ++i) {
			int u, v; rd(u, v);
			Add(u, v); Add(v, u);
		}
		Dfs(1);
		//for(int i = 1; i <= n; ++i) printf("%d ", hvy[i]);
		//printf("\n");
		Solve(1, 1);
		for(int i = 1; i <= n; ++i)
			printf("%d\n", ans[i]);
	} return 0;
}


#include <bits/stdc++.h>

template <typename T> inline void rd(T& x) {
	int si = 1; char c = getchar(); x = 0;
	while(!isdigit(c)) si = c == '-' ? -1 : si, c = getchar();
	while(isdigit(c)) x = x * 10 + c - 48, c = getchar();
	x *= si;
}
template <typename T, typename... Args>
inline void rd(T& x, Args&... args) { rd(x); rd(args...); }
#define fi first
#define se second
#define mkp std::make_pair
#define V std::vector
#define H std::priority_queue
typedef long long ll;
typedef double ff;
typedef std::pair <int, int> pii;
const int kN = 1e6 + 5, kInf = 0x3f3f3f3f;
const ll kMod = 998244353, kInfLL = 0x3f3f3f3f3f3f3f3fLL;

int n, dep[kN], hvy[kN], fa[kN], ans[kN];
std::vector <int> E[kN], f[kN];
void Add(int u, int v) { E[u].push_back(v); }

void Dfs(int cur) {
	dep[cur] = 1;
	for(auto i : E[cur])
		if(i != fa[cur]) {
			fa[i] = cur; Dfs(i);
			dep[cur] = std::max(dep[i] + 1, dep[cur]);
			if(dep[i] > dep[hvy[cur]])
				hvy[cur] = i;
		}
}
int Solve(int cur, int tar) {
	//printf("%d %d %d\n", cur, tar, hvy[cur]);
	int cur_ans;
	if(hvy[cur]) cur_ans = Solve(hvy[cur], cur);
	else cur_ans = 0;
	for(auto i : E[cur]) {
		if(i != fa[cur] && i != hvy[cur]) {
			Solve(i, i);
		//	if(f[cur].size() < f[i].size()) exit(0);
		//	std::cout << cur << " " << i << " " << f[cur].size() << " " << f[i].size() << std::endl;
			for(int j = 0; j < f[i].size(); ++j) {
				f[cur][j] += f[i][j];
				if(f[cur][j] > f[cur][cur_ans] ||
					(f[cur][j] == f[cur][cur_ans] && j > cur_ans)) cur_ans = j;
			}
			f[i].resize(0);
		}
	}
	f[cur].push_back(1);
	if(f[cur][cur_ans] == 1) cur_ans = f[cur].size() - 1;
	ans[cur] = f[cur].size() - cur_ans - 1;
	//printf("%d : ", cur);
	//for(auto i : f[cur]) printf("%d ", i);
	//printf("\n");
	std::swap(f[cur], f[tar]);
	return cur_ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
#endif
	int T = 1; //rd(T);
	while(T--) {
		rd(n);
		for(int i = 1; i < n; ++i) {
			int u, v; rd(u, v);
			Add(u, v); Add(v, u);
		}
		Dfs(1);
		//for(int i = 1; i <= n; ++i) printf("%d ", hvy[i]);
		//printf("\n");
		Solve(1, 1);
		for(int i = 1; i <= n; ++i)
			printf("%d\n", ans[i]);
	} return 0;
}


#include <bits/stdc++.h>

template <typename T> inline void rd(T& x) {
	int si = 1; char c = getchar(); x = 0;
	while(!isdigit(c)) si = c == '-' ? -1 : si, c = getchar();
	while(isdigit(c)) x = x * 10 + c - 48, c = getchar();
	x *= si;
}
template <typename T, typename... Args>
inline void rd(T& x, Args&... args) { rd(x); rd(args...); }
#define fi first
#define se second
#define mkp std::make_pair
#define V std::vector
#define H std::priority_queue
typedef long long ll;
typedef double ff;
typedef std::pair <int, int> pii;
const int kN = 1e6 + 5, kInf = 0x3f3f3f3f;
const ll kMod = 998244353, kInfLL = 0x3f3f3f3f3f3f3f3fLL;

int n, dep[kN], hvy[kN], fa[kN], ans[kN];
std::vector <int> E[kN], f[kN];
void Add(int u, int v) { E[u].push_back(v); }

void Dfs(int cur) {
	for(auto i : E[cur])
		if(i != fa[cur]) {
			dep[i] = dep[cur] + 1;
			fa[i] = cur; Dfs(i);
			if(dep[i] > dep[hvy[cur]])
				hvy[cur] = i;
		}
}
int Solve(int cur, int tar) {
	//printf("%d %d %d\n", cur, tar, hvy[cur]);
	int cur_ans;
	if(hvy[cur]) cur_ans = Solve(hvy[cur], cur);
	else cur_ans = 0;
	for(auto i : E[cur]) {
		if(i != fa[cur] && i != hvy[cur]) {
			Solve(i, i);
		//	std::cout << cur << " " << i << " " << f[cur].size() << " " << f[i].size() << std::endl;
			for(int j = 0; j < f[i].size(); ++j) {
				f[cur][j] += f[i][j];
				if(f[cur][j] > f[cur][cur_ans] ||
					(f[cur][j] == f[cur][cur_ans] && j > cur_ans)) cur_ans = j;
			}
			f[i].resize(0);
		}
	}
	f[cur].push_back(1);
	if(f[cur][cur_ans] == 1) cur_ans = f[cur].size() - 1;
	ans[cur] = f[cur].size() - cur_ans - 1;
	//printf("%d : ", cur);
	//for(auto i : f[cur]) printf("%d ", i);
	//printf("\n");
	std::swap(f[cur], f[tar]);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
#endif
	int T = 1; //rd(T);
	while(T--) {
		rd(n);
		for(int i = 1; i < n; ++i) {
			int u, v; rd(u, v);
			Add(u, v); Add(v, u);
		}
		Dfs(1);
		//for(int i = 1; i <= n; ++i) printf("%d ", hvy[i]);
		//printf("\n");
		Solve(1, 1);
		for(int i = 1; i <= n; ++i)
			printf("%d\n", ans[i]);
	} return 0;
}


#include <bits/stdc++.h>

template <typename T> inline void rd(T& x) {
	int si = 1; char c = getchar(); x = 0;
	while(!isdigit(c)) si = c == '-' ? -1 : si, c = getchar();
	while(isdigit(c)) x = x * 10 + c - 48, c = getchar();
	x *= si;
}
template <typename T, typename... Args>
inline void rd(T& x, Args&... args) { rd(x); rd(args...); }
#define fi first
#define se second
#define mkp std::make_pair
#define V std::vector
#define H std::priority_queue
typedef long long ll;
typedef double ff;
typedef std::pair <int, int> pii;
const int kN = 1e6 + 5, kInf = 0x3f3f3f3f;
const ll kMod = 998244353, kInfLL = 0x3f3f3f3f3f3f3f3fLL;

int n, dep[kN], hvy[kN], fa[kN], ans[kN];
std::vector <int> E[kN], f[kN];
void Add(int u, int v) { E[u].push_back(v); }

void Dfs(int cur) {
	for(auto i : E[cur])
		if(i != fa[cur]) {
			dep[i] = dep[cur] + 1;
			fa[i] = cur; Dfs(i);
			if(dep[i] > dep[hvy[cur]])
				hvy[cur] = i;
		}
}
int Solve(int cur, int tar) {
	//printf("%d %d %d\n", cur, tar, hvy[cur]);
	int cur_ans;
	if(hvy[cur]) cur_ans = Solve(hvy[cur], cur);
	else cur_ans = 0;
	for(auto i : E[cur]) {
		if(i != fa[cur] && i != hvy[cur]) {
			Solve(i, i);
		//	std::cout << cur << " " << i << " " << f[cur].size() << " " << f[i].size() << std::endl;
			for(int j = 0; j < f[i].size(); ++j) {
				f[cur][j] += f[i][j];
				if(f[cur][j] > f[cur][cur_ans] ||
					(f[cur][j] == f[cur][cur_ans] && j > cur_ans)) cur_ans = j;
			}
			f[i].resize(0);
		}
	}
	f[cur].push_back(1);
	if(f[cur][cur_ans] == 1) cur_ans = f[cur].size() - 1;
	ans[cur] = f[cur].size() - cur_ans - 1;
	//printf("%d : ", cur);
	//for(auto i : f[cur]) printf("%d ", i);
	//printf("\n");
	std::swap(f[cur], f[tar]);
	return cur_ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
#endif
	int T = 1; //rd(T);
	while(T--) {
		rd(n);
		for(int i = 1; i < n; ++i) {
			int u, v; rd(u, v);
			Add(u, v); Add(v, u);
		}
		Dfs(1);
		//for(int i = 1; i <= n; ++i) printf("%d ", hvy[i]);
		//printf("\n");
		Solve(1, 1);
		for(int i = 1; i <= n; ++i)
			printf("%d\n", ans[i]);
	} return 0;
}


#include <bits/stdc++.h>

template <typename T> inline void rd(T& x) {
	int si = 1; char c = getchar(); x = 0;
	while(!isdigit(c)) si = c == '-' ? -1 : si, c = getchar();
	while(isdigit(c)) x = x * 10 + c - 48, c = getchar();
	x *= si;
}
template <typename T, typename... Args>
inline void rd(T& x, Args&... args) { rd(x); rd(args...); }
#define fi first
#define se second
#define mkp std::make_pair
#define V std::vector
#define H std::priority_queue
typedef long long ll;
typedef double ff;
typedef std::pair <int, int> pii;
const int kN = 1e6 + 5, kInf = 0x3f3f3f3f;
const ll kMod = 998244353, kInfLL = 0x3f3f3f3f3f3f3f3fLL;

int n, dep[kN], hvy[kN], fa[kN], ans[kN];
std::vector <int> E[kN], f[kN];
void Add(int u, int v) { E[u].push_back(v); }

void Dfs(int cur) {
	dep[cur] = 1;
	for(auto i : E[cur])
		if(i != fa[cur]) {
			fa[i] = cur; Dfs(i);
			dep[cur] = std::max(dep[i] + 1, dep[cur]);
			if(dep[i] > dep[hvy[cur]])
				hvy[cur] = i;
		}
}
int Solve(int cur, int tar) {
	//printf("%d %d %d\n", cur, tar, hvy[cur]);
	int cur_ans;
	if(hvy[cur]) cur_ans = Solve(hvy[cur], cur);
	else cur_ans = 0;
	for(auto i : E[cur]) {
		if(i != fa[cur] && i != hvy[cur]) {
			Solve(i, i);
			if(f[cur].size() < f[i].size()) exit(0);
		//	std::cout << cur << " " << i << " " << f[cur].size() << " " << f[i].size() << std::endl;
			for(int j = 0; j < f[i].size(); ++j) {
				f[cur][j] += f[i][j];
				if(f[cur][j] > f[cur][cur_ans] ||
					(f[cur][j] == f[cur][cur_ans] && j > cur_ans)) cur_ans = j;
			}
			f[i].resize(0);
		}
	}
	f[cur].push_back(1);
	if(f[cur][cur_ans] == 1) cur_ans = f[cur].size() - 1;
	ans[cur] = f[cur].size() - cur_ans - 1;
	//printf("%d : ", cur);
	//for(auto i : f[cur]) printf("%d ", i);
	//printf("\n");
	std::swap(f[cur], f[tar]);
	return cur_ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
#endif
	int T = 1; //rd(T);
	while(T--) {
		rd(n);
		for(int i = 1; i < n; ++i) {
			int u, v; rd(u, v);
			Add(u, v); Add(v, u);
		}
		Dfs(1);
		//for(int i = 1; i <= n; ++i) printf("%d ", hvy[i]);
		//printf("\n");
		Solve(1, 1);
		for(int i = 1; i <= n; ++i)
			printf("%d\n", ans[i]);
	} return 0;
}


//I_l_M_P
#include <bits/stdc++.h>

#ifdef FAST_ALLOCATOR_MEMORY
int allocator_pos = 0;
char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
inline void * operator new ( size_t n ) {
  char *res = allocator_memory + allocator_pos;
  allocator_pos += n;
  assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  return (void *)res;
}
inline void operator delete ( void * ) noexcept { }
//inline void * operator new [] ( size_t ) { assert(0); }
//inline void operator delete [] ( void * ) { assert(0); }
#endif

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s);
inline bool isEof();
inline int getChar();
inline int peekChar();
inline bool seekEof();
inline void skipBlanks();

template <class T> inline void writeInt(T x, char end = 0, int len = -1);
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

static struct buffer_flusher_t {
  ~buffer_flusher_t() {
    flush();
  }
} buffer_flusher;

static const int buf_size = 4096;

static unsigned char buf[buf_size];
static int buf_len = 0, buf_pos = 0;

inline bool isEof() {
  if (buf_pos == buf_len) {
    buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
    if (buf_pos == buf_len)
      return 1;
  }
  return 0;
}

inline int getChar() {
  return isEof() ? -1 : buf[buf_pos++];
}

inline int peekChar() {
  return isEof() ? -1 : buf[buf_pos];
}

inline bool seekEof() {
  int c;
  while ((c = peekChar()) != -1 && c <= 32)
    buf_pos++;
  return c == -1;
}

inline void skipBlanks() {
  while (!isEof() && buf[buf_pos] <= 32U)
    buf_pos++;
}

inline int readChar() {
  int c = getChar();
  while (c != -1 && c <= 32)
    c = getChar();
  return c;
}

inline int readUInt() {
  int c = readChar(), x = 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return x;
}

template <class T>
inline T readInt() {
  int s = 1, c = readChar();
  T x = 0;
  if (c == '-')
    s = -1, c = getChar();
  else if (c == '+')
    c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return s == 1 ? x : -x;
}

inline double readDouble() {
  int s = 1, c = readChar();
  double x = 0;
  if (c == '-')
    s = -1, c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  if (c == '.') {
    c = getChar();
    double coef = 1;
    while ('0' <= c && c <= '9')
      x += (c - '0') * (coef *= 1e-1), c = getChar();
  }
  return s == 1 ? x : -x;
}

inline void readWord(char *s) {
  int c = readChar();
  while (c > 32)
    *s++ = c, c = getChar();
  *s = 0;
}

inline bool readLine(char *s) {
  int c = getChar();
  while (c != '\n' && c != -1)
    *s++ = c, c = getChar();
  *s = 0;
  return c != -1;
}

static int write_buf_pos = 0;
static char write_buf[buf_size];

inline void writeChar( int x ) {
  if (write_buf_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  write_buf[write_buf_pos++] = x;
}

inline void flush() {
  if (write_buf_pos) {
    fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
    fflush(stdout);
  }
}

template <class T>
inline void writeInt(T x, char end, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n < output_len)
    s[n++] = '0';
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}

inline void writeWord(const char *s) {
  while (*s)
    writeChar(*s++);
}

inline void writeDouble(double x, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  int t = (int)x;
  writeInt(t), x -= t;
  writeChar('.');
  for (int i = output_len - 1; i > 0; i--) {
    x *= 10;
    t = std::min(9, (int)x);
    writeChar('0' + t), x -= t;
  }
  x *= 10;
  t = std::min(9, (int)(x + 0.5));
  writeChar('0' + t);
}

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

//#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

using namespace std;

typedef long double ld;
typedef long long ll;

#define _ inline
#define f first
#define sec second
#define pb push_back
#define ins insert
#define ers erase
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
//#define int ll

#define right rightt
#define left leftt
#define prev prevv
#define next nextt

mt19937 Random(time(0));

const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
const int P = 239017;
const long long BINF = 1e18 + 10;

const int N = 1e6 + 10;
const int SZ = 1e3;

struct query {
  int l, r, num;
  query(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {};
};

bool cmp(query a, query b) {
  return a.l / SZ < b.l / SZ || (a.l / SZ == b.l / SZ && a.r < b.r);
}

vector<int> g[N], order;
int sz[N], h[N], cnt[N], ans[N], cur_max;

void dfs(int v, int parent = 0, int cur_h = 0) {
  order.pb(v);
  h[v] = cur_h;
  sz[v] = 1;
  for (auto u : g[v]) {
    if (u != parent) {
      dfs(u, v, cur_h + 1);
      sz[v] += sz[u];
    }
  }
}

set<pair<int, int> > have;

void add(int ind) {
  int c = h[order[ind]];
  have.ers({cnt[c], -c});
  cnt[c]++;
  have.ins({cnt[c], -c});
}

void del(int ind) {
  int c = h[order[ind]];
  have.ers({cnt[c], -c});
  cnt[c]--;
  have.ins({cnt[c], -c});
}

void source() {
  int n = readInt();
  for (int i = 1; i < n; i++) {
    int u = readInt(), v = readInt();
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  vector<query> q;
  for (int i = 0; i < (int)order.size(); i++) {
    q.emplace_back(i, i + sz[order[i]] - 1, order[i]);
  }
  sort(all(q), cmp);
  pair<int, int> cur;
  for (int i = 0; i < n; i++) {
    if (!i) {
      for (int j = q[i].l; j <= q[i].r; j++) {
        add(j);
      }
      cur = {q[i].l, q[i].r};
    }
    else {
      while (cur.sec < q[i].r) {
        cur.sec++;
        add(cur.sec);
      }
      while (cur.sec > q[i].r) {
        del(cur.sec);
        cur.sec--;
      }
      while (cur.f < q[i].l) {
        del(cur.f);
        cur.f++;
      }
      while (cur.f > q[i].l) {
        cur.f--;
        add(cur.f);
      }
    }
    ans[q[i].num] = -have.rbegin()->sec;
  }
  for (int i = 1; i <= n; i++) {
    writeInt(ans[i] - h[i], '\n');
  }
}

signed main() {
#ifdef extraterrestrial_
  assert(freopen("input.txt", "r", stdin));
  //assert(freopen("output.txt", "w", stdout));
#else
#define TASK ""
  //assert(freopen(TASK".in", "r", stdin));
  //assert(freopen(TASK".out", "w", stdout));
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  source();
#ifdef extraterrestrial_
  writeWord("\n\t");
  writeDouble((double)clock() / (double)CLOCKS_PER_SEC, 10);
  writeWord(" seconds\n\n");
#endif
  return 0;
}


//I_l_M_P
#include <bits/stdc++.h>

#ifdef FAST_ALLOCATOR_MEMORY
int allocator_pos = 0;
char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
inline void * operator new ( size_t n ) {
  char *res = allocator_memory + allocator_pos;
  allocator_pos += n;
  assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  return (void *)res;
}
inline void operator delete ( void * ) noexcept { }
//inline void * operator new [] ( size_t ) { assert(0); }
//inline void operator delete [] ( void * ) { assert(0); }
#endif

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s);
inline bool isEof();
inline int getChar();
inline int peekChar();
inline bool seekEof();
inline void skipBlanks();

template <class T> inline void writeInt(T x, char end = 0, int len = -1);
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

static struct buffer_flusher_t {
  ~buffer_flusher_t() {
    flush();
  }
} buffer_flusher;

static const int buf_size = 4096;

static unsigned char buf[buf_size];
static int buf_len = 0, buf_pos = 0;

inline bool isEof() {
  if (buf_pos == buf_len) {
    buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
    if (buf_pos == buf_len)
      return 1;
  }
  return 0;
}

inline int getChar() {
  return isEof() ? -1 : buf[buf_pos++];
}

inline int peekChar() {
  return isEof() ? -1 : buf[buf_pos];
}

inline bool seekEof() {
  int c;
  while ((c = peekChar()) != -1 && c <= 32)
    buf_pos++;
  return c == -1;
}

inline void skipBlanks() {
  while (!isEof() && buf[buf_pos] <= 32U)
    buf_pos++;
}

inline int readChar() {
  int c = getChar();
  while (c != -1 && c <= 32)
    c = getChar();
  return c;
}

inline int readUInt() {
  int c = readChar(), x = 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return x;
}

template <class T>
inline T readInt() {
  int s = 1, c = readChar();
  T x = 0;
  if (c == '-')
    s = -1, c = getChar();
  else if (c == '+')
    c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return s == 1 ? x : -x;
}

inline double readDouble() {
  int s = 1, c = readChar();
  double x = 0;
  if (c == '-')
    s = -1, c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  if (c == '.') {
    c = getChar();
    double coef = 1;
    while ('0' <= c && c <= '9')
      x += (c - '0') * (coef *= 1e-1), c = getChar();
  }
  return s == 1 ? x : -x;
}

inline void readWord(char *s) {
  int c = readChar();
  while (c > 32)
    *s++ = c, c = getChar();
  *s = 0;
}

inline bool readLine(char *s) {
  int c = getChar();
  while (c != '\n' && c != -1)
    *s++ = c, c = getChar();
  *s = 0;
  return c != -1;
}

static int write_buf_pos = 0;
static char write_buf[buf_size];

inline void writeChar( int x ) {
  if (write_buf_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  write_buf[write_buf_pos++] = x;
}

inline void flush() {
  if (write_buf_pos) {
    fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
    fflush(stdout);
  }
}

template <class T>
inline void writeInt(T x, char end, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n < output_len)
    s[n++] = '0';
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}

inline void writeWord(const char *s) {
  while (*s)
    writeChar(*s++);
}

inline void writeDouble(double x, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  int t = (int)x;
  writeInt(t), x -= t;
  writeChar('.');
  for (int i = output_len - 1; i > 0; i--) {
    x *= 10;
    t = std::min(9, (int)x);
    writeChar('0' + t), x -= t;
  }
  x *= 10;
  t = std::min(9, (int)(x + 0.5));
  writeChar('0' + t);
}

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

//#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

using namespace std;

typedef long double ld;
typedef long long ll;

#define _ inline
#define f first
#define sec second
#define pb push_back
#define ins insert
#define ers erase
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
//#define int ll

#define right rightt
#define left leftt
#define prev prevv
#define next nextt

mt19937 Random(time(0));

const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
const int P = 239017;
const long long BINF = 1e18 + 10;

const int N = 1e6 + 10;
const int SZ = 1e3;

struct query {
  int l, r, num;
  query(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {};
};

bool cmp(query a, query b) {
  return a.l / SZ < b.l / SZ || (a.l / SZ == b.l / SZ && a.r < b.r);
}

vector<int> g[N], order;
int sz[N], h[N], cnt[N], ans[N];
pair<int, int> best = {0, -INF};
//vector<int> rlb;
//vector<pair<int, int> > rlb_best;

int rlb[N];
pair<int, int> rlb_best[N];
int r1, r2;

void dfs(int v, int parent = 0, int cur_h = 0) {
  order.pb(v);
  h[v] = cur_h;
  sz[v] = 1;
  for (auto u : g[v]) {
    if (u != parent) {
      dfs(u, v, cur_h + 1);
      sz[v] += sz[u];
    }
  }
}

void add(int ind) {
  int c = h[order[ind]];
  cnt[c]++;
  rlb_best[r2++] = best;
  rlb[r1++] = c;
  best = max(best, {cnt[c], -c});
}

void get_rollback() {
  cnt[rlb[--r1]]--;
  best = rlb_best[--r2];
}

void source() {
  int n = readInt();
  for (int i = 1; i < n; i++) {
    int u = readInt(), v = readInt();
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  vector<query> q;
  for (int i = 0; i < (int)order.size(); i++) {
    q.emplace_back(i, i + sz[order[i]] - 1, order[i]);
  }
  sort(all(q), cmp);
  pair<int, int> cur;
  for (int i = 0; i < n; i++) {
    if (!i || q[i].l / SZ != q[i - 1].l / SZ) {
      while (r1 > 0) {
        get_rollback();
      }
      for (int j = q[i].l; j <= q[i].r; j++) {
        if (j / SZ != q[i].l / SZ) {
          add(j);
        }
      }
      cur = {q[i].l, q[i].r};
    }
    else {
      while (cur.sec < q[i].r) {
        cur.sec++;
        if (cur.sec / SZ != q[i].l / SZ) add(cur.sec);
      }
      cur.f = q[i].l;
    }
    int kek = 0;
    for (int j = q[i].l; j <= q[i].r; j++) {
      if (j / SZ != q[i].l / SZ) {
        break;
      }
      kek++;
      add(j);
    }
    ans[q[i].num] = -best.sec;
    for (int j = 0; j < kek; j++) {
      get_rollback();
    }
  }
  for (int i = 1; i <= n; i++) {
    writeInt(ans[i] - h[i], '\n');
  }
}

signed main() {
#ifdef extraterrestrial_
  assert(freopen("input.txt", "r", stdin));
  //assert(freopen("output.txt", "w", stdout));
#else
#define TASK ""
  //assert(freopen(TASK".in", "r", stdin));
  //assert(freopen(TASK".out", "w", stdout));
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  source();
#ifdef extraterrestrial_
  writeWord("\n\t");
  writeDouble((double)clock() / (double)CLOCKS_PER_SEC, 10);
  writeWord(" seconds\n\n");
#endif
  return 0;
}


//I_l_M_P
#include <bits/stdc++.h>

#ifdef FAST_ALLOCATOR_MEMORY
int allocator_pos = 0;
char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
inline void * operator new ( size_t n ) {
  char *res = allocator_memory + allocator_pos;
  allocator_pos += n;
  assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  return (void *)res;
}
inline void operator delete ( void * ) noexcept { }
//inline void * operator new [] ( size_t ) { assert(0); }
//inline void operator delete [] ( void * ) { assert(0); }
#endif

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s);
inline bool isEof();
inline int getChar();
inline int peekChar();
inline bool seekEof();
inline void skipBlanks();

template <class T> inline void writeInt(T x, char end = 0, int len = -1);
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

static struct buffer_flusher_t {
  ~buffer_flusher_t() {
    flush();
  }
} buffer_flusher;

static const int buf_size = 4096;

static unsigned char buf[buf_size];
static int buf_len = 0, buf_pos = 0;

inline bool isEof() {
  if (buf_pos == buf_len) {
    buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
    if (buf_pos == buf_len)
      return 1;
  }
  return 0;
}

inline int getChar() {
  return isEof() ? -1 : buf[buf_pos++];
}

inline int peekChar() {
  return isEof() ? -1 : buf[buf_pos];
}

inline bool seekEof() {
  int c;
  while ((c = peekChar()) != -1 && c <= 32)
    buf_pos++;
  return c == -1;
}

inline void skipBlanks() {
  while (!isEof() && buf[buf_pos] <= 32U)
    buf_pos++;
}

inline int readChar() {
  int c = getChar();
  while (c != -1 && c <= 32)
    c = getChar();
  return c;
}

inline int readUInt() {
  int c = readChar(), x = 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return x;
}

template <class T>
inline T readInt() {
  int s = 1, c = readChar();
  T x = 0;
  if (c == '-')
    s = -1, c = getChar();
  else if (c == '+')
    c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return s == 1 ? x : -x;
}

inline double readDouble() {
  int s = 1, c = readChar();
  double x = 0;
  if (c == '-')
    s = -1, c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  if (c == '.') {
    c = getChar();
    double coef = 1;
    while ('0' <= c && c <= '9')
      x += (c - '0') * (coef *= 1e-1), c = getChar();
  }
  return s == 1 ? x : -x;
}

inline void readWord(char *s) {
  int c = readChar();
  while (c > 32)
    *s++ = c, c = getChar();
  *s = 0;
}

inline bool readLine(char *s) {
  int c = getChar();
  while (c != '\n' && c != -1)
    *s++ = c, c = getChar();
  *s = 0;
  return c != -1;
}

static int write_buf_pos = 0;
static char write_buf[buf_size];

inline void writeChar( int x ) {
  if (write_buf_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  write_buf[write_buf_pos++] = x;
}

inline void flush() {
  if (write_buf_pos) {
    fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
    fflush(stdout);
  }
}

template <class T>
inline void writeInt(T x, char end, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n < output_len)
    s[n++] = '0';
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}

inline void writeWord(const char *s) {
  while (*s)
    writeChar(*s++);
}

inline void writeDouble(double x, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  int t = (int)x;
  writeInt(t), x -= t;
  writeChar('.');
  for (int i = output_len - 1; i > 0; i--) {
    x *= 10;
    t = std::min(9, (int)x);
    writeChar('0' + t), x -= t;
  }
  x *= 10;
  t = std::min(9, (int)(x + 0.5));
  writeChar('0' + t);
}

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

//#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

using namespace std;

typedef long double ld;
typedef long long ll;

#define _ inline
#define f first
#define sec second
#define pb push_back
#define ins insert
#define ers erase
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
//#define int ll

#define right rightt
#define left leftt
#define prev prevv
#define next nextt

mt19937 Random(time(0));

const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
const int P = 239017;
const long long BINF = 1e18 + 10;

const int N = 1e6 + 10;
const int SZ = 5e2;

struct query {
  int l, r, num;
  query(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {};
};

bool cmp(query a, query b) {
  return a.l / SZ < b.l / SZ || (a.l / SZ == b.l / SZ && a.r < b.r);
}

vector<int> g[N], order;
int sz[N], h[N], cnt[N], ans[N];
pair<int, int> best = {0, -INF};

int rlb[N];
pair<int, int> rlb_best[N];
int r1, r2;

void dfs(int v, int parent = 0, int cur_h = 0) {
  order.pb(v);
  h[v] = cur_h;
  sz[v] = 1;
  for (auto u : g[v]) {
    if (u != parent) {
      dfs(u, v, cur_h + 1);
      sz[v] += sz[u];
    }
  }
}

void add(int ind) {
  int c = h[order[ind]];
  cnt[c]++;
  rlb_best[r2++] = best;
  rlb[r1++] = c;
  best = max(best, {cnt[c], -c});
}

void get_rollback() {
  cnt[rlb[--r1]]--;
  best = rlb_best[--r2];
}

void source() {
  int n = readInt();
  for (int i = 1; i < n; i++) {
    int u = readInt(), v = readInt();
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  vector<query> q;
  for (int i = 0; i < (int)order.size(); i++) {
    q.emplace_back(i, i + sz[order[i]] - 1, order[i]);
  }
  sort(all(q), cmp);
  pair<int, int> cur;
  for (int i = 0; i < n; i++) {
    if (!i || q[i].l / SZ != q[i - 1].l / SZ) {
      while (r1 > 0) {
        get_rollback();
      }
      for (int j = q[i].l; j <= q[i].r; j++) {
        if (j / SZ != q[i].l / SZ) {
          add(j);
        }
      }
      cur = {q[i].l, q[i].r};
    }
    else {
      while (cur.sec < q[i].r) {
        cur.sec++;
        if (cur.sec / SZ != q[i].l / SZ) add(cur.sec);
      }
      cur.f = q[i].l;
    }
    int kek = 0;
    for (int j = q[i].l; j <= q[i].r; j++) {
      if (j / SZ != q[i].l / SZ) {
        break;
      }
      kek++;
      add(j);
    }
    ans[q[i].num] = -best.sec;
    for (int j = 0; j < kek; j++) {
      get_rollback();
    }
  }
  for (int i = 1; i <= n; i++) {
    writeInt(ans[i] - h[i], '\n');
  }
}

signed main() {
#ifdef extraterrestrial_
  assert(freopen("input.txt", "r", stdin));
  //assert(freopen("output.txt", "w", stdout));
#else
#define TASK ""
  //assert(freopen(TASK".in", "r", stdin));
  //assert(freopen(TASK".out", "w", stdout));
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  source();
#ifdef extraterrestrial_
  writeWord("\n\t");
  writeDouble((double)clock() / (double)CLOCKS_PER_SEC, 10);
  writeWord(" seconds\n\n");
#endif
  return 0;
}


//I_l_M_P
#include <bits/stdc++.h>

#ifdef FAST_ALLOCATOR_MEMORY
int allocator_pos = 0;
char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
inline void * operator new ( size_t n ) {
  char *res = allocator_memory + allocator_pos;
  allocator_pos += n;
  assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  return (void *)res;
}
inline void operator delete ( void * ) noexcept { }
//inline void * operator new [] ( size_t ) { assert(0); }
//inline void operator delete [] ( void * ) { assert(0); }
#endif

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s);
inline bool isEof();
inline int getChar();
inline int peekChar();
inline bool seekEof();
inline void skipBlanks();

template <class T> inline void writeInt(T x, char end = 0, int len = -1);
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

static struct buffer_flusher_t {
  ~buffer_flusher_t() {
    flush();
  }
} buffer_flusher;

static const int buf_size = 4096;

static unsigned char buf[buf_size];
static int buf_len = 0, buf_pos = 0;

inline bool isEof() {
  if (buf_pos == buf_len) {
    buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
    if (buf_pos == buf_len)
      return 1;
  }
  return 0;
}

inline int getChar() {
  return isEof() ? -1 : buf[buf_pos++];
}

inline int peekChar() {
  return isEof() ? -1 : buf[buf_pos];
}

inline bool seekEof() {
  int c;
  while ((c = peekChar()) != -1 && c <= 32)
    buf_pos++;
  return c == -1;
}

inline void skipBlanks() {
  while (!isEof() && buf[buf_pos] <= 32U)
    buf_pos++;
}

inline int readChar() {
  int c = getChar();
  while (c != -1 && c <= 32)
    c = getChar();
  return c;
}

inline int readUInt() {
  int c = readChar(), x = 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return x;
}

template <class T>
inline T readInt() {
  int s = 1, c = readChar();
  T x = 0;
  if (c == '-')
    s = -1, c = getChar();
  else if (c == '+')
    c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return s == 1 ? x : -x;
}

inline double readDouble() {
  int s = 1, c = readChar();
  double x = 0;
  if (c == '-')
    s = -1, c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  if (c == '.') {
    c = getChar();
    double coef = 1;
    while ('0' <= c && c <= '9')
      x += (c - '0') * (coef *= 1e-1), c = getChar();
  }
  return s == 1 ? x : -x;
}

inline void readWord(char *s) {
  int c = readChar();
  while (c > 32)
    *s++ = c, c = getChar();
  *s = 0;
}

inline bool readLine(char *s) {
  int c = getChar();
  while (c != '\n' && c != -1)
    *s++ = c, c = getChar();
  *s = 0;
  return c != -1;
}

static int write_buf_pos = 0;
static char write_buf[buf_size];

inline void writeChar( int x ) {
  if (write_buf_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  write_buf[write_buf_pos++] = x;
}

inline void flush() {
  if (write_buf_pos) {
    fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
    fflush(stdout);
  }
}

template <class T>
inline void writeInt(T x, char end, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n < output_len)
    s[n++] = '0';
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}

inline void writeWord(const char *s) {
  while (*s)
    writeChar(*s++);
}

inline void writeDouble(double x, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  int t = (int)x;
  writeInt(t), x -= t;
  writeChar('.');
  for (int i = output_len - 1; i > 0; i--) {
    x *= 10;
    t = std::min(9, (int)x);
    writeChar('0' + t), x -= t;
  }
  x *= 10;
  t = std::min(9, (int)(x + 0.5));
  writeChar('0' + t);
}

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

//#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

using namespace std;

typedef long double ld;
typedef long long ll;

#define _ inline
#define f first
#define sec second
#define pb push_back
#define ins insert
#define ers erase
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
//#define int ll

#define right rightt
#define left leftt
#define prev prevv
#define next nextt

mt19937 Random(time(0));

const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
const int P = 239017;
const long long BINF = 1e18 + 10;

const int N = 1e6 + 10;
const int SZ = 3e3;

struct query {
  int l, r, num;
  query(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {};
};

bool cmp(query a, query b) {
  return a.l / SZ < b.l / SZ || (a.l / SZ == b.l / SZ && a.r < b.r);
}

vector<int> g[N], order;
int sz[N], h[N], cnt[N], ans[N], cur_max;

void dfs(int v, int parent = 0, int cur_h = 0) {
  order.pb(v);
  h[v] = cur_h;
  sz[v] = 1;
  for (auto u : g[v]) {
    if (u != parent) {
      dfs(u, v, cur_h + 1);
      sz[v] += sz[u];
    }
  }
}

set<pair<int, int> > have;

void add(int ind) {
  int c = h[order[ind]];
  have.ers({cnt[c], -c});
  cnt[c]++;
  have.ins({cnt[c], -c});
}

void del(int ind) {
  int c = h[order[ind]];
  have.ers({cnt[c], -c});
  cnt[c]--;
  have.ins({cnt[c], -c});
}

void source() {
  int n = readInt();
  for (int i = 1; i < n; i++) {
    int u = readInt(), v = readInt();
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  vector<query> q;
  for (int i = 0; i < (int)order.size(); i++) {
    q.emplace_back(i, i + sz[order[i]] - 1, order[i]);
  }
  sort(all(q), cmp);
  pair<int, int> cur;
  for (int i = 0; i < n; i++) {
    if (!i) {
      for (int j = q[i].l; j <= q[i].r; j++) {
        add(j);
      }
      cur = {q[i].l, q[i].r};
    }
    else {
      while (cur.sec < q[i].r) {
        cur.sec++;
        add(cur.sec);
      }
      while (cur.sec > q[i].r) {
        del(cur.sec);
        cur.sec--;
      }
      while (cur.f < q[i].l) {
        del(cur.f);
        cur.f++;
      }
      while (cur.f > q[i].l) {
        cur.f--;
        add(cur.f);
      }
    }
    ans[q[i].num] = -have.rbegin()->sec;
  }
  for (int i = 1; i <= n; i++) {
    writeInt(ans[i] - h[i], '\n');
  }
}

signed main() {
#ifdef extraterrestrial_
  assert(freopen("input.txt", "r", stdin));
  //assert(freopen("output.txt", "w", stdout));
#else
#define TASK ""
  //assert(freopen(TASK".in", "r", stdin));
  //assert(freopen(TASK".out", "w", stdout));
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  source();
#ifdef extraterrestrial_
  writeWord("\n\t");
  writeDouble((double)clock() / (double)CLOCKS_PER_SEC, 10);
  writeWord(" seconds\n\n");
#endif
  return 0;
}


//I_l_M_P
#include <bits/stdc++.h>

#ifdef FAST_ALLOCATOR_MEMORY
int allocator_pos = 0;
char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
inline void * operator new ( size_t n ) {
  char *res = allocator_memory + allocator_pos;
  allocator_pos += n;
  assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  return (void *)res;
}
inline void operator delete ( void * ) noexcept { }
//inline void * operator new [] ( size_t ) { assert(0); }
//inline void operator delete [] ( void * ) { assert(0); }
#endif

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s);
inline bool isEof();
inline int getChar();
inline int peekChar();
inline bool seekEof();
inline void skipBlanks();

template <class T> inline void writeInt(T x, char end = 0, int len = -1);
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

static struct buffer_flusher_t {
  ~buffer_flusher_t() {
    flush();
  }
} buffer_flusher;

static const int buf_size = 4096;

static unsigned char buf[buf_size];
static int buf_len = 0, buf_pos = 0;

inline bool isEof() {
  if (buf_pos == buf_len) {
    buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
    if (buf_pos == buf_len)
      return 1;
  }
  return 0;
}

inline int getChar() {
  return isEof() ? -1 : buf[buf_pos++];
}

inline int peekChar() {
  return isEof() ? -1 : buf[buf_pos];
}

inline bool seekEof() {
  int c;
  while ((c = peekChar()) != -1 && c <= 32)
    buf_pos++;
  return c == -1;
}

inline void skipBlanks() {
  while (!isEof() && buf[buf_pos] <= 32U)
    buf_pos++;
}

inline int readChar() {
  int c = getChar();
  while (c != -1 && c <= 32)
    c = getChar();
  return c;
}

inline int readUInt() {
  int c = readChar(), x = 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return x;
}

template <class T>
inline T readInt() {
  int s = 1, c = readChar();
  T x = 0;
  if (c == '-')
    s = -1, c = getChar();
  else if (c == '+')
    c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return s == 1 ? x : -x;
}

inline double readDouble() {
  int s = 1, c = readChar();
  double x = 0;
  if (c == '-')
    s = -1, c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  if (c == '.') {
    c = getChar();
    double coef = 1;
    while ('0' <= c && c <= '9')
      x += (c - '0') * (coef *= 1e-1), c = getChar();
  }
  return s == 1 ? x : -x;
}

inline void readWord(char *s) {
  int c = readChar();
  while (c > 32)
    *s++ = c, c = getChar();
  *s = 0;
}

inline bool readLine(char *s) {
  int c = getChar();
  while (c != '\n' && c != -1)
    *s++ = c, c = getChar();
  *s = 0;
  return c != -1;
}

static int write_buf_pos = 0;
static char write_buf[buf_size];

inline void writeChar( int x ) {
  if (write_buf_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  write_buf[write_buf_pos++] = x;
}

inline void flush() {
  if (write_buf_pos) {
    fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
    fflush(stdout);
  }
}

template <class T>
inline void writeInt(T x, char end, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n < output_len)
    s[n++] = '0';
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}

inline void writeWord(const char *s) {
  while (*s)
    writeChar(*s++);
}

inline void writeDouble(double x, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  int t = (int)x;
  writeInt(t), x -= t;
  writeChar('.');
  for (int i = output_len - 1; i > 0; i--) {
    x *= 10;
    t = std::min(9, (int)x);
    writeChar('0' + t), x -= t;
  }
  x *= 10;
  t = std::min(9, (int)(x + 0.5));
  writeChar('0' + t);
}

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

//#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

using namespace std;

typedef long double ld;
typedef long long ll;

#define _ inline
#define f first
#define sec second
#define pb push_back
#define ins insert
#define ers erase
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
//#define int ll

#define right rightt
#define left leftt
#define prev prevv
#define next nextt

mt19937 Random(time(0));

const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
const int P = 239017;
const long long BINF = 1e18 + 10;

const int N = 1e6 + 10;
const int SZ = 2e3;

struct query {
  int l, r, num;
  query(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {};
};

bool cmp(query a, query b) {
  return a.l / SZ < b.l / SZ || (a.l / SZ == b.l / SZ && a.r < b.r);
}

vector<int> g[N], order;
int sz[N], h[N], cnt[N], ans[N];
pair<int, int> best = {0, -INF};
//vector<int> rlb;
//vector<pair<int, int> > rlb_best;

int rlb[N];
pair<int, int> rlb_best[N];
int r1, r2;

void dfs(int v, int parent = 0, int cur_h = 0) {
  order.pb(v);
  h[v] = cur_h;
  sz[v] = 1;
  for (auto u : g[v]) {
    if (u != parent) {
      dfs(u, v, cur_h + 1);
      sz[v] += sz[u];
    }
  }
}

void add(int ind) {
  int c = h[order[ind]];
  cnt[c]++;
  rlb_best[r2++] = best;
  rlb[r1++] = c;
  best = max(best, {cnt[c], -c});
}

void get_rollback() {
  cnt[rlb[--r1]]--;
  best = rlb_best[--r2];
}

void source() {
  int n = readInt();
  for (int i = 1; i < n; i++) {
    int u = readInt(), v = readInt();
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  vector<query> q;
  for (int i = 0; i < (int)order.size(); i++) {
    q.emplace_back(i, i + sz[order[i]] - 1, order[i]);
  }
  sort(all(q), cmp);
  pair<int, int> cur;
  for (int i = 0; i < n; i++) {
    if (!i || q[i].l / SZ != q[i - 1].l / SZ) {
      while (r1 > 0) {
        get_rollback();
      }
      for (int j = q[i].l; j <= q[i].r; j++) {
        if (j / SZ != q[i].l / SZ) {
          add(j);
        }
      }
      cur = {q[i].l, q[i].r};
    }
    else {
      while (cur.sec < q[i].r) {
        cur.sec++;
        if (cur.sec / SZ != q[i].l / SZ) add(cur.sec);
      }
      cur.f = q[i].l;
    }
    int kek = 0;
    for (int j = q[i].l; j <= q[i].r; j++) {
      if (j / SZ != q[i].l / SZ) {
        break;
      }
      kek++;
      add(j);
    }
    ans[q[i].num] = -best.sec;
    for (int j = 0; j < kek; j++) {
      get_rollback();
    }
  }
  for (int i = 1; i <= n; i++) {
    writeInt(ans[i] - h[i], '\n');
  }
}

signed main() {
#ifdef extraterrestrial_
  assert(freopen("input.txt", "r", stdin));
  //assert(freopen("output.txt", "w", stdout));
#else
#define TASK ""
  //assert(freopen(TASK".in", "r", stdin));
  //assert(freopen(TASK".out", "w", stdout));
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  source();
#ifdef extraterrestrial_
  writeWord("\n\t");
  writeDouble((double)clock() / (double)CLOCKS_PER_SEC, 10);
  writeWord(" seconds\n\n");
#endif
  return 0;
}


//I_l_M_P
#include <bits/stdc++.h>

#ifdef FAST_ALLOCATOR_MEMORY
int allocator_pos = 0;
char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
inline void * operator new ( size_t n ) {
  char *res = allocator_memory + allocator_pos;
  allocator_pos += n;
  assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  return (void *)res;
}
inline void operator delete ( void * ) noexcept { }
//inline void * operator new [] ( size_t ) { assert(0); }
//inline void operator delete [] ( void * ) { assert(0); }
#endif

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s);
inline bool isEof();
inline int getChar();
inline int peekChar();
inline bool seekEof();
inline void skipBlanks();

template <class T> inline void writeInt(T x, char end = 0, int len = -1);
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

static struct buffer_flusher_t {
  ~buffer_flusher_t() {
    flush();
  }
} buffer_flusher;

static const int buf_size = 4096;

static unsigned char buf[buf_size];
static int buf_len = 0, buf_pos = 0;

inline bool isEof() {
  if (buf_pos == buf_len) {
    buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
    if (buf_pos == buf_len)
      return 1;
  }
  return 0;
}

inline int getChar() {
  return isEof() ? -1 : buf[buf_pos++];
}

inline int peekChar() {
  return isEof() ? -1 : buf[buf_pos];
}

inline bool seekEof() {
  int c;
  while ((c = peekChar()) != -1 && c <= 32)
    buf_pos++;
  return c == -1;
}

inline void skipBlanks() {
  while (!isEof() && buf[buf_pos] <= 32U)
    buf_pos++;
}

inline int readChar() {
  int c = getChar();
  while (c != -1 && c <= 32)
    c = getChar();
  return c;
}

inline int readUInt() {
  int c = readChar(), x = 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return x;
}

template <class T>
inline T readInt() {
  int s = 1, c = readChar();
  T x = 0;
  if (c == '-')
    s = -1, c = getChar();
  else if (c == '+')
    c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return s == 1 ? x : -x;
}

inline double readDouble() {
  int s = 1, c = readChar();
  double x = 0;
  if (c == '-')
    s = -1, c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  if (c == '.') {
    c = getChar();
    double coef = 1;
    while ('0' <= c && c <= '9')
      x += (c - '0') * (coef *= 1e-1), c = getChar();
  }
  return s == 1 ? x : -x;
}

inline void readWord(char *s) {
  int c = readChar();
  while (c > 32)
    *s++ = c, c = getChar();
  *s = 0;
}

inline bool readLine(char *s) {
  int c = getChar();
  while (c != '\n' && c != -1)
    *s++ = c, c = getChar();
  *s = 0;
  return c != -1;
}

static int write_buf_pos = 0;
static char write_buf[buf_size];

inline void writeChar( int x ) {
  if (write_buf_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  write_buf[write_buf_pos++] = x;
}

inline void flush() {
  if (write_buf_pos) {
    fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
    fflush(stdout);
  }
}

template <class T>
inline void writeInt(T x, char end, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n < output_len)
    s[n++] = '0';
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}

inline void writeWord(const char *s) {
  while (*s)
    writeChar(*s++);
}

inline void writeDouble(double x, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  int t = (int)x;
  writeInt(t), x -= t;
  writeChar('.');
  for (int i = output_len - 1; i > 0; i--) {
    x *= 10;
    t = std::min(9, (int)x);
    writeChar('0' + t), x -= t;
  }
  x *= 10;
  t = std::min(9, (int)(x + 0.5));
  writeChar('0' + t);
}

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

using namespace std;

typedef long double ld;
typedef long long ll;

#define _ inline
#define f first
#define sec second
#define pb push_back
#define ins insert
#define ers erase
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
//#define int ll

#define right rightt
#define left leftt
#define prev prevv
#define next nextt

mt19937 Random(time(0));

const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
const int P = 239017;
const long long BINF = 1e18 + 10;

const int N = 1e6 + 10;
const int SZ = 1e3;

struct query {
  int l, r, num;
  query(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {};
};

bool cmp(query a, query b) {
  return a.l / SZ < b.l / SZ || (a.l / SZ == b.l / SZ && a.r < b.r);
}

vector<int> g[N], order;
int sz[N], h[N], cnt[N], ans[N];
pair<int, int> best = {0, -INF};

int rlb[N];
pair<int, int> rlb_best[N];
int r1, r2;

void dfs(int v, int parent = 0, int cur_h = 0) {
  order.pb(v);
  h[v] = cur_h;
  sz[v] = 1;
  for (auto u : g[v]) {
    if (u != parent) {
      dfs(u, v, cur_h + 1);
      sz[v] += sz[u];
    }
  }
}

void add(int ind) {
  int c = h[order[ind]];
  cnt[c]++;
  rlb_best[r2++] = best;
  rlb[r1++] = c;
  best = max(best, {cnt[c], -c});
}

void get_rollback() {
  cnt[rlb[--r1]]--;
  best = rlb_best[--r2];
}

void source() {
  int n = readInt();
  for (int i = 1; i < n; i++) {
    int u = readInt(), v = readInt();
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  bool t = true;
  for (int i = 1; i <= n; i++) {
    if (h[i] != i - 1) {
      t = false;
      break;
    }
  }
  if (t) {
    for (int i = 0; i < n; i++) {
      writeInt(0, '\n');
    }
    exit(0);
  }
  vector<query> q;
  for (int i = 0; i < (int)order.size(); i++) {
    q.emplace_back(i, i + sz[order[i]] - 1, order[i]);
  }
  sort(all(q), cmp);
  pair<int, int> cur;
  for (int i = 0; i < n; i++) {
    if (!i || q[i].l / SZ != q[i - 1].l / SZ) {
      while (r1 > 0) {
        get_rollback();
      }
      for (int j = q[i].l; j <= q[i].r; j++) {
        if (j / SZ != q[i].l / SZ) {
          add(j);
        }
      }
      cur = {q[i].l, q[i].r};
    }
    else {
      while (cur.sec < q[i].r) {
        cur.sec++;
        if (cur.sec / SZ != q[i].l / SZ) add(cur.sec);
      }
      cur.f = q[i].l;
    }
    int kek = 0;
    for (int j = q[i].l; j <= q[i].r; j++) {
      if (j / SZ != q[i].l / SZ) {
        break;
      }
      kek++;
      add(j);
    }
    ans[q[i].num] = -best.sec;
    for (int j = 0; j < kek; j++) {
      get_rollback();
    }
  }
  for (int i = 1; i <= n; i++) {
    writeInt(ans[i] - h[i], '\n');
  }
}

signed main() {
#ifdef extraterrestrial_
  assert(freopen("input.txt", "r", stdin));
  //assert(freopen("output.txt", "w", stdout));
#else
#define TASK ""
  //assert(freopen(TASK".in", "r", stdin));
  //assert(freopen(TASK".out", "w", stdout));
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  source();
#ifdef extraterrestrial_
  writeWord("\n\t");
  writeDouble((double)clock() / (double)CLOCKS_PER_SEC, 10);
  writeWord(" seconds\n\n");
#endif
  return 0;
}


//I_l_M_P
#include <bits/stdc++.h>

#ifdef FAST_ALLOCATOR_MEMORY
int allocator_pos = 0;
char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
inline void * operator new ( size_t n ) {
  char *res = allocator_memory + allocator_pos;
  allocator_pos += n;
  assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  return (void *)res;
}
inline void operator delete ( void * ) noexcept { }
//inline void * operator new [] ( size_t ) { assert(0); }
//inline void operator delete [] ( void * ) { assert(0); }
#endif

template <class T = int> inline T readInt();
inline double readDouble();
inline int readUInt();
inline int readChar();
inline void readWord(char *s);
inline bool readLine(char *s);
inline bool isEof();
inline int getChar();
inline int peekChar();
inline bool seekEof();
inline void skipBlanks();

template <class T> inline void writeInt(T x, char end = 0, int len = -1);
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void writeDouble(double x, int len = 0);
inline void flush();

static struct buffer_flusher_t {
  ~buffer_flusher_t() {
    flush();
  }
} buffer_flusher;

static const int buf_size = 4096;

static unsigned char buf[buf_size];
static int buf_len = 0, buf_pos = 0;

inline bool isEof() {
  if (buf_pos == buf_len) {
    buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
    if (buf_pos == buf_len)
      return 1;
  }
  return 0;
}

inline int getChar() {
  return isEof() ? -1 : buf[buf_pos++];
}

inline int peekChar() {
  return isEof() ? -1 : buf[buf_pos];
}

inline bool seekEof() {
  int c;
  while ((c = peekChar()) != -1 && c <= 32)
    buf_pos++;
  return c == -1;
}

inline void skipBlanks() {
  while (!isEof() && buf[buf_pos] <= 32U)
    buf_pos++;
}

inline int readChar() {
  int c = getChar();
  while (c != -1 && c <= 32)
    c = getChar();
  return c;
}

inline int readUInt() {
  int c = readChar(), x = 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return x;
}

template <class T>
inline T readInt() {
  int s = 1, c = readChar();
  T x = 0;
  if (c == '-')
    s = -1, c = getChar();
  else if (c == '+')
    c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  return s == 1 ? x : -x;
}

inline double readDouble() {
  int s = 1, c = readChar();
  double x = 0;
  if (c == '-')
    s = -1, c = getChar();
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getChar();
  if (c == '.') {
    c = getChar();
    double coef = 1;
    while ('0' <= c && c <= '9')
      x += (c - '0') * (coef *= 1e-1), c = getChar();
  }
  return s == 1 ? x : -x;
}

inline void readWord(char *s) {
  int c = readChar();
  while (c > 32)
    *s++ = c, c = getChar();
  *s = 0;
}

inline bool readLine(char *s) {
  int c = getChar();
  while (c != '\n' && c != -1)
    *s++ = c, c = getChar();
  *s = 0;
  return c != -1;
}

static int write_buf_pos = 0;
static char write_buf[buf_size];

inline void writeChar( int x ) {
  if (write_buf_pos == buf_size)
    fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  write_buf[write_buf_pos++] = x;
}

inline void flush() {
  if (write_buf_pos) {
    fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
    fflush(stdout);
  }
}

template <class T>
inline void writeInt(T x, char end, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  char s[24];
  int n = 0;
  while (x || !n)
    s[n++] = '0' + x % 10, x /= 10;
  while (n < output_len)
    s[n++] = '0';
  while (n--)
    writeChar(s[n]);
  if (end)
    writeChar(end);
}

inline void writeWord(const char *s) {
  while (*s)
    writeChar(*s++);
}

inline void writeDouble(double x, int output_len) {
  if (x < 0)
    writeChar('-'), x = -x;
  int t = (int)x;
  writeInt(t), x -= t;
  writeChar('.');
  for (int i = output_len - 1; i > 0; i--) {
    x *= 10;
    t = std::min(9, (int)x);
    writeChar('0' + t), x -= t;
  }
  x *= 10;
  t = std::min(9, (int)(x + 0.5));
  writeChar('0' + t);
}

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

//#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

using namespace std;

typedef long double ld;
typedef long long ll;

#define _ inline
#define f first
#define sec second
#define pb push_back
#define ins insert
#define ers erase
#define mk make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
//#define int ll

#define right rightt
#define left leftt
#define prev prevv
#define next nextt

mt19937 Random(time(0));

const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
const int P = 239017;
const long long BINF = 1e18 + 10;

const int N = 1e6 + 10;
const int SZ = 1e3;

struct query {
  int l, r, num;
  query(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {};
};

bool cmp(query a, query b) {
  return a.l / SZ < b.l / SZ || (a.l / SZ == b.l / SZ && a.r < b.r);
}

vector<int> g[N], order;
int sz[N], h[N], cnt[N], ans[N];
pair<int, int> best = {0, -INF};
vector<int> rlb;
vector<pair<int, int> > rlb_best;

void dfs(int v, int parent = 0, int cur_h = 0) {
  order.pb(v);
  h[v] = cur_h;
  sz[v] = 1;
  for (auto u : g[v]) {
    if (u != parent) {
      dfs(u, v, cur_h + 1);
      sz[v] += sz[u];
    }
  }
}

void add(int ind) {
  int c = h[order[ind]];
  cnt[c]++;
  rlb_best.pb(best);
  rlb.pb(c);
  best = max(best, {cnt[c], -c});
}

void get_rollback() {
  cnt[rlb.back()]--;
  rlb.pop_back();
  best = rlb_best.back();
  rlb_best.pop_back();
}

void source() {
  int n = readInt();
  for (int i = 1; i < n; i++) {
    int u = readInt(), v = readInt();
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  vector<query> q;
  for (int i = 0; i < (int)order.size(); i++) {
    q.emplace_back(i, i + sz[order[i]] - 1, order[i]);
  }
  sort(all(q), cmp);
  pair<int, int> cur;
  for (int i = 0; i < n; i++) {
    if (!i || q[i].l / SZ != q[i - 1].l / SZ) {
      while (!rlb.empty()) {
        get_rollback();
      }
      for (int j = q[i].l; j <= q[i].r; j++) {
        if (j / SZ != q[i].l / SZ) {
          add(j);
        }
      }
      cur = {q[i].l, q[i].r};
    }
    else {
      while (cur.sec < q[i].r) {
        cur.sec++;
        if (cur.sec / SZ != q[i].l / SZ) add(cur.sec);
      }
      cur.f = q[i].l;
    }
    int kek = 0;
    for (int j = q[i].l; j <= q[i].r; j++) {
      if (j / SZ != q[i].l / SZ) {
        break;
      }
      kek++;
      add(j);
    }
    ans[q[i].num] = -best.sec;
    for (int j = 0; j < kek; j++) {
      get_rollback();
    }
  }
  for (int i = 1; i <= n; i++) {
    writeInt(ans[i] - h[i], '\n');
  }
}

signed main() {
#ifdef extraterrestrial_
  assert(freopen("input.txt", "r", stdin));
  //assert(freopen("output.txt", "w", stdout));
#else
#define TASK ""
  //assert(freopen(TASK".in", "r", stdin));
  //assert(freopen(TASK".out", "w", stdout));
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  source();
#ifdef extraterrestrial_
  writeWord("\n\t");
  writeDouble((double)clock() / (double)CLOCKS_PER_SEC, 10);
  writeWord(" seconds\n\n");
#endif
  return 0;
}